I have route file defined as follows...
Route::get('pincodes/export', 'PincodeController#export'); Route::resource('pincodes','PincodeController');
I have a controller called 'PincodeController' where the default functions works without any issue. I wanted to add a new function called 'export' in this controller. I tried {{ route('pincodes.export') }} in my blade file to generate link to this page... and it throws the following error...
Facade\Ignition\Exceptions\ViewException Route [pincodes.export] not defined. (View: /Users/tst/Desktop/www/laravel/project/resources/views/pincode/index.blade.php)
but if i access the url directly in browser it works fine. why is that ?
You need to specifically name the route:
Route::get('pincodes/export', 'PincodeController#export')->name('pincodes.export');
If you are using Laravel 5.5+, change export route like this
Route::get('pincodes/export', 'PincodeController#export')->name('export');
use route function in your blade file like
route('export')
Related
In my web.php,
Route::get('studentmarksheet/addit','StmarksheetController#addit');
Route::resource('studentmarksheet','StmarksheetController');
I created a custom function addit() inside StmarksheetController. I have a form inside a view where I need to pass values to that addit function. For default functions inside my resoource controller, I used to call by
but, while trying to pass form values in addit function, it says route not defined. What exactly should I write? I have tried
{{route('studentmarksheet/addit')}}
{{route('studentmarksheet#addit')}}
and various other combinations.
I am a total beginner and I don't even know if am questioning this correctly. Please share your answers/suggestions/tips and so on, I would love to read them all.
First you need to name a route
Route::get('studentmarksheet/addit','StmarksheetController#addit')->name('stmarksheet.addit');
Then call it
{{route('stmarksheet.addit')}}
Resource route are named for you by Laravel
You didn't set any name for the addit route. So, you need to write your route as like.
Route::get('studentmarksheet/addit','StmarksheetController#addit')->name('stmarksheet.addit');
And then you can get it in the view.
{{ route('studentmarksheet#addit') }}
I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.
I'm fairly new to Laravel. I'm having a problem with routing.
Route::group(['prefix'=>'api/v1'],function(){
Route::resource('results','RequestController');
Route::get('results/getByName/{name}','RequestController#getByName');
Route::get('results/getLastTen','RequestController#getLastTen');
});
The problem is that the last route under prefix api/v1 does not work. When I call it it shows nothing, not even any error.
The code at the requestController is:
public function getLastTen(){
$results=DB::table('latest_random_trends')->limit(10)->get();
return $results;
}
Everything is alright with the code on the controller since it works when I call it from the routes.php file outside of the prefix 'api/v1' like this:
Route::get('results/getLastTen','RequestController#getLastTen');
but when it is inside the prefix it does not work unless I add a variable to it like this:
Route::get('results/getLastTen/{var}','RequestController#getLastTen');
Since you have a Route::resource above it, I think what's happening is that the show method on Resource Controller is getting the route instead of the one you wrote.
Try one the following:
Exclude the show method if you're not going to use it
Route::resource('results','RequestController', ['except' => 'show']);
Move your custom route above the resource route
Route::group(['prefix'=>'api/v1'],function(){
Route::get('results/getLastTen','RequestController#getLastTen');
Route::resource('results','RequestController');
Route::get('results/getByName/{name}', 'RequestController#getByName');
});
For more information, check out the show action on Laravel Docs
according to the Laravel 5.5 docs, there is a named() method for accessing route names:
if ($request->route()->named('profile')) {
//
}
Inspecting the source, I learned that this named method simply fetches the 'as' property of the action object:
$this->action['as']
My problem is I'm stuck using Laravel 5.2, which doesn't have a named() method. I can't use route()->action['as'] in my blade template, because the actionobject is protected. Is there an equivalent getter method in 5.2, to check the name of the current route? I want to flow control in my blade.php file like this:
#if(route()->action['as'] == 'blog.edit')
//
#endif
Maybe I missed it, but I didn't see anything in the Laravel 5.2 docs: https://laravel.com/docs/5.2/routing#named-routes
I succeeded in checking the route using
#if(request()->is('blog/add'))
//
#endif
But that is using the route URI. I prefer to use the route name instead as it's less clunky
answer from Gitter courtesy of Ben Johnson:
#if(Route::currentRouteName() == 'blog.edit')
//
#endif
I have the followning method in my controller:
public function showQualityResult($qualityData) {
return $qualityData;
}
When clicked on a link , i want that method to be invoked , so i have the following in my view file:
Submited Quality Check
Also, I have the following route setup:
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult');
But having the below line of code:
Submited Quality Check
Does't really work , i get the following error in the frontEnd:
Now how can i solve this problem , and why am i getting this error of Route not defined , even though i have the route defined ??
route() helper uses route name to build URL, so you need to use it like this:
route('quality-result.show', session('quality-data'));
And set a name for the route:
Route::get('/showQualityResult', ['as' => 'quality-result.show', 'uses' => 'QualityCheckController#showQualityResult']);
Or:
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult')->name('quality-result.show');
The route function generates a URL for the given named route
https://laravel.com/docs/5.3/routing#named-routes
If you don't want to use route names, use url() instead of route()
The route() helper looks for a named route, not the path.
https://laravel.com/docs/5.3/routing#named-routes
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult')
->name('showQualityResult');
Should fix the issue for you.
In my case I had simply made a stupid mistake.
Further down in my code I had another named route (properly named with a unique name) but an identical path to the one Laravel told me it couldn't find.
A quick update to the path fixed it.