Laravel 8 Resource Route Controller Example Tutorial

Laravel 8 resource route, controller example. In this tutorial, we will show you how to create a resource route, controller, API resource route, and API resource controller in laravel 8 app. And as well as how to use this controller and routes in laravel 8 app.

And also learn how to create resource routes and controllers using the PHP artisan make command in laravel 8 app.

This laravel 8 resource route controller tutorial will give you a simple example of laravel 8 resource route, API routes, controller, and API controller

If you are making a crud application in Laravel 8 app. And you use the resource controller and routes. So you will not have to define the entire routes in the routes file.

Laravel 8 Resource Controller And Routes 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

Now, we will show you how to create simple and resource controller in laravel 8 app using artisan command.

So open your terminal and navigate to your laravel 8 app directory. After that, use the below-given command to create simple and resource controller in laravel 8 app.

Create a Simple Controller

To create simple controller in laravel 8 app by the following command:

php artisan make:controller BOOKController

The above command will create a simple controller file inside app/http/controllers directory. When you open it, you will look like:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


class BOOKController extends Controller
{

}

Create a Resource Controller

To create a Resource controller in laravel 8 app by the following command:

php artisan make:controller CRUDController --resource

The above command will create a resource controller file inside app/http/controllers directory. When you open it, you will look like:

<?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)
    {


    }
}

Note that, You saw above 2 commands to create simple controller and resource controller. Also saw that there is a slight difference in the command to create both controllers. By running the command of simple controller, a simple controller file is created. But when you execute the command to create the resource controller. Then resource controller contains bydefault index (), create (), edit (), update (), destroy () method inside it.

Create a Resource Controller with Model

To create Resource controller in laravel 8 app by the following command:

php artisan make:controller BOOKController --resource --model=book

The above command will create resource controller with model file. And controller file has located inside app/http/controllers directory. And Model file has been located inside app/Models directory.

If you open controller file, you will look like:

<?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:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Crud extends Model
{
    use HasFactory;
}

2:- Routes

Now, we will show you how to define or create simple and resource rotues in laravel 8 app.

So, Navigate to your laravel 8 app directory. And open web.php file, which is placed inside routes directory.

Create Simple Routes

Create Simple routes for crud application in laravel 8 app:

<?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

Create resource routes for crud application in laravel 8 app:

<?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:

php artisan route:list

The following command will display resource routes methods:

+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+
| 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 |
+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+

Note that, If you use simple routes. Then you need to define all routes in routes file. But if you use resource route. Then you just have to write the resource in front of the single route. As you saw in the above-given example.

3:- API Controller and Routes

To create resource controller by using the following command:

Create Resource Controller

To create simple controller in laravel 8 app by the following command:

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 it, you will look like:

<?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

Now, navigate to routes directory and open api.php. Then you can define resource api routes in laravel 8 app:

use App\Http\Controllers\API\BOOKController;

Route::resource('book', [BOOKController::class]);

Conclusion

In this laravel 8 resource route, controller example, you have learned how to create resource controller and api resource controller using artisan command in laravel 8 app. And how to define resource routes and api resource routes in laravel 8 app.

Recommended Laravel Posts

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *