Laravel 10 Resource Route and Controller Tutorial Example

Laravel 10 Resource Route and Controller Tutorial Example

If you are working in Laravel App. Don’t know about the resource route end controller. Then this tutorial is for you. In this tutorial, you will learn how to create a resource route, controller, API resource route, and API resource controller in Laravel 10 app using the artisan command via cmd.

If you do resource controller in laravel by php artisan command. So this command creates 5 methods in your resource controller. Like index(), create(), update(), edit() and destroy().

How to Create Resource Route and Controller In Laravel Applications

By using the following ways, you can create resource controller and routes in laravel 10 apps using artisan commands:

  • Controller Using Artisan Command
    • Create a Simple Controller
    • Create a Resource Controller
    • Create a Resource Controller with Model
  • Create Routes
    • Create Simple Routes
    • Create Resource Routes
  • API Controller and Routes

Controller Using Artisan Command

Now, you can use the following artisan command to create a simple and resource controller in Laravel 10 apps.

So open your terminal and navigate to your Laravel 10 app directory. Then execute the below-given commands to create a simple and resource controller in Laravel 10 app.

Create a Simple Controller

To create simple controller in Laravel 10 app by the following artisan 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 10 app by the following artisan 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 10 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;
}

Create Routes

Now, you need to define or create simple and resource rotues in Laravel 10 app.

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

Create Simple Routes

Create Simple routes for crud application in Laravel 10 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 10 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.

API Controller and Routes

To create resource controller by using the following command:

Create Resource Controller

To create simple controller in Laravel 10 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 10 app:

use App\Http\Controllers\API\BOOKController;

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

Conclusion

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

Recommended Laravel Posts

AuthorAdmin

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 *