How to use Middleware in Controller Laravel?

Laravel 6 , Laravel

Nicesnippets

543

13-01-2020


Hi Guys,

In this example,I will leran you how to use middleware in controller in laravel. i will let you know to call middleware from controller method using $this-middleware.you can simply and easy to use the middleware in controller laravel.

Middleware provide a convenient mechanism for filtering HTTP requests entering your application.

i will show you constructor for adding middleware for each method like index, create, store, edit, delete, show and update of controller.

you can also use middleware in controller like as bellow example:

Example :

<?php

namespace App\Http\Controllers;

use App\Product;

use Illuminate\Http\Request;

class ProductController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('product-list', ['only' => ['index']]);

$this->middleware('product-create', ['only' => ['index','create','store']]);

$this->middleware('product-update', ['only' => ['index','edit','update']]);

$this->middleware('product-delete', ['only' => ['index','delete']]);

}

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$products = Product::latest()->paginate(5);

return view('products.index',compact('products'))

->with('i', (request()->input('page', 1) - 1) * 5);

}

/**

* Show the form for creating a new resource.

*

* @return \Illuminate\Http\Response

*/

public function create()

{

return view('products.create');

}

/**

* Store a newly created resource in storage.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{

$request->validate([

'name' => 'required',

'detail' => 'required',

]);

Product::create($request->all());

return redirect()->route('products.index')

->with('success','Product created successfully.');

}

/**

* Display the specified resource.

*

* @param \App\Product $product

* @return \Illuminate\Http\Response

*/

public function show(Product $product)

{

return view('products.show',compact('product'));

}

/**

* Show the form for editing the specified resource.

*

* @param \App\Product $product

* @return \Illuminate\Http\Response

*/

public function edit(Product $product)

{

return view('products.edit',compact('product'));

}

/**

* Update the specified resource in storage.

*

* @param \Illuminate\Http\Request $request

* @param \App\Product $product

* @return \Illuminate\Http\Response

*/

public function update(Request $request, Product $product)

{

$request->validate([

'name' => 'required',

'detail' => 'required',

]);

$product->update($request->all());

return redirect()->route('products.index')

->with('success','Product updated successfully');

}

/**

* Remove the specified resource from storage.

*

* @param \App\Product $product

* @return \Illuminate\Http\Response

*/

public function destroy(Product $product)

{

$product->delete();

return redirect()->route('products.index')

->with('success','Product deleted successfully');

}

}

It will help you...


Recommended Posts