Resource Controller Tutorial in Laravel 11
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
echo "All Users Data";
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
echo "Add New User Page";
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
echo "Details of User : $id";
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
echo "Edit Page";
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
Above File is app\Http\Controllers\UserController.php FileBelow File is app\Http\Controllers\CommentController.php File
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CommentController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
echo "All comments";
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
echo "Add New Comment";
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
Below File is routes\web.php File
<?php
use App\Http\Controllers\CommentController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
// Mostly Resource Controller is used for CRUD Operation
Route::resource("users", UserController::class);
// If we want to use specific methods only then we can use as shown below :
// Route::resource("users", UserController::class)->only([
// "create","update","show"
// ]);
// If we want to use all methods except some specific methods then we can use as shown below :
// Route::resource("users", UserController::class)->except([
// "update","show"
// ]);
// If we want to Rename method names then we can use as shown below :
// Route::resource("users", UserController::class)->names([
// "create" => "users.build",
// "show" => "users.view"
// ]);
// To use nested route using "shallow" method as shown below :
Route::resource("users.comments", CommentController::class)->shallow();
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment