To build the create, update, and delete features in Laravel, follow these steps:
Define Route
Start by defining routes to handle the create, update, and delete actions.
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');
In the above example, we define routes for creating a user, storing a user, editing a user, updating a user, and deleting a user.
Define the Controller
Next, define the methods in the controller to handle the requests from the routes.
<?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.');
}
}
In each method, you can perform the corresponding actions such as displaying a form, storing new data, updating existing data, and deleting data.
Create User Interface
Create the user interface (views
) to display forms and view data. For example:
List (views/users/index.blade.php
):
@foreach($users as $user)
<p>{{ $user->name }} - {{ $user->email }}</p>
@endforeach
Edit Form (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>
Edit Form (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>
Handle Data
In the store and update methods in the controller, you can use Eloquent methods to store and update data in the database.
Display Messages
Finally, you can display success or error messages to the user after performing the create, update, and delete actions.
- Use Laravel Session to display success or error messages in views.
By following these steps, you have successfully built the create, update, and delete features in Laravel.