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

Javascript Laravel Ajax Autocomplete Using Select2

Laravel Customized Autocomplete Options Using Select2

July 18, 2022July 18, 2022

In this article we will learn to use Laravel Customized Autocomplete Options Using Select2. Select2 is useful when we want live search of bulk data or to convert the existing select boz with multi features like search, multi select and options customizations. Autocomplete search is mostly work of javascript and…

Read More
Php Google Map with Multiple Marker and Info Box in Laravel

Google Map with Multiple Marker and Info Box in Laravel

August 30, 2022March 16, 2024

In this article we will learn to integrate google map with multiple marker and info box in laravel. Google maps are used to navigate the user location or indicate the location of business and any user. Sometimes In our application we want to show the multiple location with some information…

Read More

How to find php.ini file location ?

August 24, 2021February 22, 2024

Sometime we installed multiple version of php on our system so it will get difficult to find php.ini file location . Let’s start with easiest way. Steps to find the php ini file on dedicated, localhost and ssh based enabled server. Then run this file in browser. As we can…

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

  • June 2025
  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version