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

Php How to run raw query in Laravel

How to run raw query laravel eloquent ?

February 12, 2022March 28, 2023

Sometimes in laravel we wanted to run raw query like select, insert, delete, alter etc. Laravel have multiple ways to run a raw query using select, prepare or statement method in db builder. In this article i will show you to run the raw query in laravel. To understand this…

Read More
Laravel Laravel Audit Integration

Exploring Laravel Audit Integration: Ensuring Data Security and Accountability

November 9, 2023March 16, 2024

In today’s digital age, data security and accountability are of utmost importance and Laravel Audit most important to keep the records of each changes. Organizations need to keep a close eye on who accesses, modifies, or deletes sensitive information within their applications. Laravel, a popular PHP framework, has a powerful…

Read More
Laravel How to rollback migration laravel

How to Rollback Migration Laravel ?

March 6, 2022February 8, 2024

In our recent article I showed you about creating migration in laravel and sometimes after creating migration we want to rollback migration laravel so in this article i will show you to rollback migration in laravel. Laravel artisan have multiple options to rollback the migration like we can rollback all…

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