In this tutorial you will learn about the Laravel 8 Resource Route Controller Example Tutorial and its application with practical example.
In this Laravel 8 resource route, controller example tutoria I will show you how to create a resource route, controller, API resource route, and API resource controller in laravel 8. In this tutorial you will learn to create resource route, controller, API resource route and API resource controller in laravel. You will also learn to use resource controller and routes in laravel 8. In this article I will also show you how to create resource routes and controllers using the PHP artisan make command in laravel 8.
Laravel Resource Route and Controller
With laravel resource controller and resource route you can quick and easily CRUD operation in laravel. In order to create CRUD operation for any resource you have to create resource routes and controller. First you need to create resource route and then need to create resource controller to generate method for insert, update, view and delete operation for any resource.
Laravel 8 Resource Route Controller Example Tutorial
- Controller Using Artisan Command
- Create a Simple Controller
- Create a Resource Controller
- Create a Resource Controller with Model
- Routes
- Create Simple Routes
- Create Resource Routes
- API Controller and Routes
1:- Controller Using Artisan Command
Use the following command to create simple and resource controller in laravel 8 app.
Create a Simple Controller
Use the following artisan command to create simple controller in laravel:
1 |
php artisan make:controller BOOKController |
The above command will create a simple controller file inside app/http/controllers directory. When you open newly created controller file it will look like:
1 2 3 4 5 6 7 8 9 10 11 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class BOOKController extends Controller { } |
Create a Resource Controller
Use the following artisan command to create resource controller in laravel:
1 |
php artisan make:controller CRUDController --resource |
The above command will create a resource controller file inside app/http/controllers directory. When you open newly created resource controller file it will look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class BOOKController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { } /** * Display the specified resource. * * @param \App\Book $book * @return \Illuminate\Http\Response */ public function show(Book $book) { } /** * Show the form for editing the specified resource. * * @param \App\Book $book * @return \Illuminate\Http\Response */ public function edit(Book $book) { } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Book $book * @return \Illuminate\Http\Response */ public function update(Request $request, Book $book) { } /** * Remove the specified resource from storage. * * @param \App\Book $book * @return \Illuminate\Http\Response */ public function destroy(Book $book) { } } |
The resource controller contains by default index (), create (), edit (), update (), destroy () method inside it.
Create a Resource Controller with Model
Use the following artisan command to create a resource controller with model file in laravel:
1 |
php artisan make:controller BOOKController --resource --model=book |
The above command will create resource controller along with a model file. The controller file has located inside app/http/controllers directory. And Model file is located inside app/Models directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; Use App\Models\Book; class BOOKController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { } /** * Display the specified resource. * * @param \App\Book $book * @return \Illuminate\Http\Response */ public function show(Book $book) { } /** * Show the form for editing the specified resource. * * @param \App\Book $book * @return \Illuminate\Http\Response */ public function edit(Book $book) { } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Book $book * @return \Illuminate\Http\Response */ public function update(Request $request, Book $book) { } /** * Remove the specified resource from storage. * * @param \App\Book $book * @return \Illuminate\Http\Response */ public function destroy(Book $book) { } } |
If you open Model file, you will look like:
1 2 3 4 5 6 7 8 9 10 11 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Crud extends Model { use HasFactory; } |
2:- Routes
Lets open “routes/web.php” file and add the following routes in it.
Create Simple Routes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\BOOKController; Route::get('books',['as'=>'books.index','uses'=>'BOOKController@index']); Route::post('books/create',['as'=>'books.store','uses'=>'BOOKController@store']); Route::get('books/edit/{id}',['as'=>'books.edit','uses'=>'BOOKController@edit']); Route::patch('books/{id}',['as'=>'books.update','uses'=>'BOOKController@update']); Route::delete('books/{id}',['as'=>'books.destroy','uses'=>'BOOKController@destroy']); Route::get('books/{id}',['as'=>'books.view','uses'=>'BOOKController@view']); |
Create Resource Routes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\CRUDController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::resource('crud', CRUDController::class); |
Then open terminal and run the following command on it:
1 |
php artisan route:list |
The following command will display resource routes methods:
1 2 3 4 5 6 7 8 9 10 11 12 |
+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+-----------+-------------------+---------------+---------------------------------------------+--------------+ | | GET|HEAD | api/user | | Closure | api,auth:api | | | GET|HEAD | books | books.index | App\Http\Controllers\BOOKController@index | web | | | POST | books | books.store | App\Http\Controllers\BOOKController@store | web | | | GET|HEAD | books/create | books.create | App\Http\Controllers\BOOKController@create | web | | | GET|HEAD | books/{book} | books.show | App\Http\Controllers\BOOKController@show | web | | | PUT|PATCH | books/{book} | books.update | App\Http\Controllers\BOOKController@update | web | | | DELETE | books/{book} | books.destroy | App\Http\Controllers\BOOKController@destroy | web | | | GET|HEAD | books/{book}/edit | books.edit | App\Http\Controllers\BOOKController@edit | web | +--------+-----------+-------------------+---------------+---------------------------------------------+--------------+ |
3:- API Controller and Routes
Create Resource Controller
Use the following artisan command to create a API resource controller:
1 |
php artisan make:controller API\BOOKController- resource |
The above command will create a simple controller file inside app/http/controllers/API directory. When you open newly created resource controller file it will look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
<?php namespace App\Http\Controllers\API; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class BOOKController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { } /** * Display the specified resource. * * @param \App\Book $book * @return \Illuminate\Http\Response */ public function show(Book $book) { } /** * Show the form for editing the specified resource. * * @param \App\Book $book * @return \Illuminate\Http\Response */ public function edit(Book $book) { } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Book $book * @return \Illuminate\Http\Response */ public function update(Request $request, Book $book) { } /** * Remove the specified resource from storage. * * @param \App\Book $book * @return \Illuminate\Http\Response */ public function destroy(Book $book) { } } |
Define Resource API Route
Lets open “routes/api.php” file and add the following routes in it.
routes/api.php
1 2 3 |
use App\Http\Controllers\API\BOOKController; Route::resource('book', [BOOKController::class]); |