Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

How to create a new function in resource controller in Laravel?

I am having a resource controller Purchase_detailsContoller. My doubt is how can I create a new function in this controller other then default functions (create, index, edit, store, show, and destroy).

Here is my config route file ,

Route::resource('Purchase_details','Purchase_detailsController');

What should I do to add a new function in Purchase_detailsContoller. In route file what should I do?

Say for example if I need to add a function public open(). Then what should I do?

0 likes
10 replies
oroalej's avatar

create a function in the controller then add it in the route.

Route::get('purchase_detauls/newFunction', Purchase_detailsController@newFunction');
Route::resource('Purchase_details','Purchase_detailsController');

or you can enclose it in Route::group

1 like
tomopongrac's avatar

In your controller you create function

    public function open()
    {
        // your code
    }

and then in your route file add

Route::get('open', 'Purchase_detailsContoller@open');
1 like
Nasmed's avatar

If you need to add additional routes to a resource controller beyond the default set of resource routes, you should define those routes before your call to Route::resource; otherwise, the routes defined by the resource method may unintentionally take precedence over your supplemental routes:

Route::get('Purchase_details/open', 'Purchase_detailsController@open');
Route::resource('Purchase_details', 'Purchase_detailsController');

Learn more here: https://laravel.com/docs/5.6/controllers#restful-supplementing-resource-controllers

1 like
rahim3070's avatar

In your route file, add

Route::get / post('order','SuperAdminController@order');

In your controller you create a function like this, if your routes is a get route then,

public function order() {

}

if your routes is a post route then,

public function order(Request $request) {

}

1 like
AbdulBazith's avatar

In the navbar I am having

<a href={{ route('Overall.open') }}> PURCHASE DETAILS

But still it shows error

"Route [Purchase_details.open] not defined."

if I need to redirect the link in my nav bar what should i give the name

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

this

route('Overall.open')

is a named route, so you need to add name() to your route definition

using @tomopongrac suggestion

Route::get('open', 'Purchase_detailsContoller@open')->name('Overall.open');

and make sure this appears in your routes before the resource route

1 like
AbdulBazith's avatar

@tomopongrac @Snapey Thank you soo much guys.. it worked for me. thank youuu..

@Snapey one doubt while posting questions in laracast sometimes my code gets not formatted in black screen why ?

1 like
Snapey's avatar

For a block of code, put ``` on a line before and after your code

for a single line, you can just indent with 4 spaces

for inline code like this just put a single ` either side of the code

AbdulBazith's avatar

@Snapey Thank you soooo much..

I have posted a question related to search please see that and reply

Please or to participate in this conversation.