Jengo Unda, Sasisha na Futa Vipengee ndani Laravel

Ili kuunda, kusasisha na kufuta vipengele katika Laravel, fuata hatua hizi:

Bainisha Route

Anza kwa kufafanua route s kushughulikia kuunda, kusasisha na kufuta vitendo.

Route::get('/users', 'UserController@index')->name('users.index');
Route::get('/users/create', 'UserController@create')->name('users.create');
Route::post('/users', 'UserController@store')->name('users.store');
Route::get('/users/{id}/edit', 'UserController@edit')->name('users.edit');
Route::put('/users/{id}', 'UserController@update')->name('users.update');
Route::delete('/users/{id}', 'UserController@destroy')->name('users.destroy');

Katika mfano hapo juu, tunafafanua route s kwa kuunda mtumiaji, kuhifadhi mtumiaji, kuhariri mtumiaji, kusasisha mtumiaji, na kufuta mtumiaji.

 

Fafanua Controller

Ifuatayo, fafanua njia katika controller kushughulikia ombi kutoka kwa route s.

<?php  
  
namespace App\Http\Controllers;  
  
use App\Models\User;  
use Illuminate\Http\Request;  
  
class UserController extends Controller  
{  
    public function index()  
    {  
        $users = User::all();  
        return view('users.index', compact('users'));  
    }  
  
    public function create()  
    {  
        return view('users.create');  
    }  
  
    public function store(Request $request)  
    {  
        $validatedData = $request->validate([  
            'name' => 'required',  
            'email' => 'required|email',  
        ]);  
  
        $user = User::create($validatedData);  
  
        return redirect()->route('users.index')->with('success', 'User created successfully.');  
    }  
  
    public function edit($id)  
    {  
        $user = User::findOrFail($id);  
        return view('users.edit', compact('user'));  
    }  
  
    public function update(Request $request, $id)  
    {  
        $validatedData = $request->validate([  
            'name' => 'required',  
            'email' => 'required|email',  
        ]);  
  
        $user = User::findOrFail($id);  
        $user->update($validatedData);  
  
        return redirect()->route('users.index')->with('success', 'User updated successfully.');  
    }  
  
    public function destroy($id)  
    {  
        $user = User::findOrFail($id);  
        $user->delete();  
  
        return redirect()->route('users.index')->with('success', 'User deleted successfully.');  
    }  
}  

Katika kila mbinu, unaweza kufanya vitendo sambamba kama vile kuonyesha fomu, kuhifadhi data mpya, kusasisha data iliyopo, na kufuta data.

 

Unda Mtumiaji Interface

Unda mtumiaji interface( views) ili kuonyesha fomu na kutazama data. Kwa mfano:

Orodha( views/users/index.blade.php):

@foreach($users as $user)  
    <p>{{ $user->name }}- {{ $user->email }}</p>  
@endforeach  

Badilisha Fomu( views/users/create.blade.php):

<form method="POST" action="{{ route('users.store') }}">  
    @csrf  
    <input type="text" name="name" placeholder="Name">  
    <input type="email" name="email" placeholder="Email">  
    <button type="submit">Create User</button>  
</form>  

Badilisha Fomu( views/users/edit.blade.php):

<form method="POST" action="{{ route('users.update', $user->id) }}">  
    @csrf  
    @method('PUT')  
    <input type="text" name="name" value="{{ $user->name }}">  
    <input type="email" name="email" value="{{ $user->email }}">  
    <button type="submit">Update User</button>  
</form>  

 

Kushughulikia Data

Katika duka na kusasisha mbinu katika controller, unaweza kutumia Mbinu za Fasaha kuhifadhi na kusasisha data katika hifadhidata.

 

Onyesha Ujumbe

Hatimaye, unaweza kuonyesha mafanikio au ujumbe wa makosa kwa mtumiaji baada ya kuunda, kusasisha na kufuta vitendo.

  • Tumia Laravel Kipindi kuonyesha mafanikio au ujumbe wa hitilafu katika mionekano.

 

Kwa kufuata hatua hizi, umefanikiwa kuunda, kusasisha na kufuta vipengele katika Laravel.