How to call middleware from controller in Laravel?

How to call middleware from controller in Laravel?

In this post we will give you information about How to call middleware from controller in Laravel?. Hear we will give you detail about How to call middleware from controller in Laravel?And how to use it also give you demo for it if it is necessary.

Do you need to call middleware from controller function in laravel app, if yes then i will show you simple example to run middleware from controller constructor using “$this->middleware”.

Actually, i was working on my laravel 5.7 project. I need acl and i used entrust package for role and permission on my project. So i created products module with resource route. I require to add permission for each route. I thought if i add permission like for product create, product edit, product delete then i have to create several routes instead of resource route. But i found way to use middleware in controller method.

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

Example:

Also see:Laravel 5.7 Middleware Tutorial With Example

<?php

namespace AppHttpControllers;

use AppProduct;

use IlluminateHttpRequest;

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 IlluminateHttpResponse

*/

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 IlluminateHttpResponse

*/

public function create()

{

return view('products.create');

}

/**

* Store a newly created resource in storage.

*

* @param IlluminateHttpRequest $request

* @return IlluminateHttpResponse

*/

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 AppProduct $product

* @return IlluminateHttpResponse

*/

public function show(Product $product)

{

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

}

/**

* Show the form for editing the specified resource.

*

* @param AppProduct $product

* @return IlluminateHttpResponse

*/

public function edit(Product $product)

{

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

}

/**

* Update the specified resource in storage.

*

* @param IlluminateHttpRequest $request

* @param AppProduct $product

* @return IlluminateHttpResponse

*/

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 AppProduct $product

* @return IlluminateHttpResponse

*/

public function destroy(Product $product)

{

$product->delete();

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

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

}

}

I hope it can help you…

Hope this code and post will helped you for implement How to call middleware from controller in Laravel?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

44  +    =  48

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US