In Laravelresource controllers are a powerful way to handle standard CRUD operations (Create, Read, Update, Deletion). The resource controller automatically creates methods for all these operations. When we create a resource controller using the Artisan command, we get predefined methods.
Resource Controller methods
1. index
Method: public function index()
Purpose: Responsible for displaying a list of all resources. For example, it can be a list of all users or products. It is usually used to display a view with all the elements.
2. create
Method: public function create()
Purpose: Responsible for displaying the form for adding a new resource. It is used to output a form where the user can enter the data of a new element.
3. store
Method: public function store(Request $request)
Purpose: Processes a request to create a new resource. Data from the form is sent here, and after validation it is saved in the database. At the end, you may be redirected to another page.
4. show
Method: public function show($id)
Purpose: It is responsible for displaying a specific resource by its identifier. It is usually used to display the details of a specific element (for example, a specific user or product).
5. edit
Method: public function edit($id)
Purpose: Responsible for displaying the edit form for an existing resource. A form with the current data for editing is displayed here.
6. update
Method: public function update(Request $request, $id)
Purpose: Processes a request to update an existing resource. Data from the editing form is sent here, and after validation, they update the corresponding resource in the database.
7. destroy
Method: public function destroy($id)
Purpose: Responsible for deleting an existing resource. It deletes the resource by ID and usually redirects the user after performing the deletion.
Example of resource controller implementation
namespace App\Http\Controllers;
use App\Models\User; // example
of the use Illuminate\Http\Request model;
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)
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
]);
User::create($request->all());
return redirect()->route('users.index')->with('success', 'User has been successfully created.');
}
public function show($id)
{
$user = User::findOrFail($id);
return view('users.show', compact('user'));
}
public function edit($id)
{
$user = User::findOrFail($id);
return view('users.edit', compact('user'));
}
public function update(Request $request, $id)
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,' . $id,
]);
$user = User::findOrFail($id);
$user->update($request->all());
return redirect()->route('users.index')->with('success', 'The user has been successfully updated.');
}
public function destroy($id)
{
$user = User::findOrFail($id);
$user->delete();
return redirect()->route('users.index')->with('success', 'The user has been successfully deleted.');
}
}
Resource controllers in Laravel simplify the implementation of the MVC design pattern, making the code more organized and maintainable. They must be used to create and manage resources in the application, corresponding to CRUD operations.