Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Creating resource controller in laravel

How to create resource controller in Laravel 8 ?

Aman Jain, November 7, 2021November 7, 2021

Resource controller is helpful when you want Create, Read, Update and Delete (CRUD) operations. If you have a model with same set of actions like crud then resource controller are useful. we can create resource controller easily using the artisan command.

php artisan make:controller ImageController --resource

Above command will generate controller ImageController in \app\Http\Controllers.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ImageController 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  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

As we can see it create some default methods for crud and we can register routes for this controller in routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;

Route::resource('images', ImageController::class);

Rout::resource will register all index, edit, show, create, store, update, delete routes.

Here is the list of routes

GET/imagesindeximages.index
GET/images/createcreateimages.create
POST/imagesstoreimages.store
GET/images/{photo}showimages.show
GET/images/{photo}/editeditimages.edit
PUT/PATCH/images/{photo}updateimages.update
DELETE/images/{photo}destroyimages.destroy

We can also define multiple resource in a single statement.

Route::resource(['images'=>ImageController::class,
                 'posts'=>PostController::class ]);

Also we can bind the middleware to it

Route::resource(['images'=>ImageController::class,
                 'posts'=>PostController::class ])->middleware('auth');

Create specific resource route

Sometime we oly want to create specific actions like create, store or index. So, Laravel also have this feature to include and exclude routes from resource using only and except methods

Route::resource(['images'=>ImageController::class])->only(['create']);
Route::resource(['images'=>ImageController::class])->only([ 'update', 'destroy']);

Api Resource controller

Above we created resource controller for web, now if we want to create the resource controller for REST api we can create in same way with artisan command

php artisan make:controller ImageController --api

In api we don’t want html action like create and edit. above command will create resource controller for api excluding method create and edit.

Also in the same way we can create route for api using apiResource method in api.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;

Route::apiResource('images', ImageController::class);
beenhere

ALSO READ

How to create controller

Related

Php Laravel controllerlaravelphp

Post navigation

Previous post
Next post

Related Posts

Laravel How to Find the laravel version and location

How to find installed laravel version?

March 5, 2022March 5, 2022

To find the installed laravel version in command line we can use artisan command and to find the laravel version in application code we can use app method. In this article i will show you to get the version of laravel version in both ways using command line and in…

Read More
Php Laravel multiple orderBy clause with example

Laravel multiple orderBy clause with example

October 6, 2022March 16, 2024

In this blog post, we will take a look at how to use the Laravel multiple orderBy clause in the Eloquent ORM. You can order query results by multiple columns using the orderBy method. This is useful when you want to sort by multiple criteria. In this post, we’ll show…

Read More
Laravel folder paths using helpers in laravel

How to get folder paths using helpers in laravel?

August 27, 2021February 22, 2024

Laravel has helpers to get folder paths using helpers in laravel and by which we can easily get root folder, public folder, assets folder, storage folder, app folder etc. Laravel has helpers to get the path of root folder, public folder, assets folder, storage folder, app folder. To get the…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version