In laravel 6.5 when using the route helper function and creating an edit link the output that i get is myurl/myroute?id=1 where before it was myurl/myroute/1/edit. How can i go back to the previous state?
this is my code, my routes are named.
Edit
If you use a route resource:
Route::resource('project', 'ProjectsController);
Then it uses {project} as the model binding so instead of id you should pass project.
{{ route('project.edit', $project) }}
Should work just fine and replace the wild card with the id of the model.
Running php artisan route:list will give you exactly the name of the parameter expected in the route :)
Related
Laravel is somehow getting the same view for 2 different routes and I can't figure out why since I'm not very familiar with Laravel.
Here's the 2 routes in question :
Route::get('/{label}', [BookController::class, 'listbyCat'])->name('bycats');
Route::get('/{id}', [BookController::class, 'singlebook'])->name('book');
The first route is the one returning the view (calling the method), that means if I switch between them it wall call singlebook() in both routes.
HTML :
Book
Category
When you try to access any resource you gonna write the route on the browser
so what if i just had two resources like those:
Route::get('/book/{label}', [BookController::class, 'listbyCat'])->name('bycats');
Route::get('/book/{id}', [BookController::class, 'singlebook'])->name('book');
And i am trying to access localhost::8080/book/5
So what tells laravel from this route that this route belongs to which one of the resources ?
Nothing !!
Cause 5 could be considered as a label or id
So you should change the route to be identifiable to laravel
Try to change one of the routes, Example:
Route::get('/book/by/{label}', [BookController::class, 'listbyCat'])->name('bycats');
Route::get('/book/{id}', [BookController::class, 'singlebook'])->name('book');
Should be obvious that when there is no other way for laravel to determine which route you want, that it will route to the first one it has in its routing table.
This would be a simple workaround that should illustrate what you are missing.
Route::get('/cat/{label}', [BookController::class, 'listbyCat'])-name('bycats');
Route::get('/book/{id}', [BookController::class, 'singlebook'])->name('book');
Artisan should be your goto tool whenever you first encounter a problem like this.
php artisan route:list
I'm using same route name for the get & post methods in route. those routes are using for the same purpose.
ex : I'm calling to load add view form using get route
Route::get('add', 'UserController#addView')->name('user.add');
then,
I'm calling to store data in that form using post route
Route::post('add', 'UserController#store')->name('user.add');
is there any issue , If I use the same route name like this?
No, you can not use the same name for 2 different routes as is stated in the documentation if you really need to name the routes you should look for different names, but if there's no need to have named routes you can have each url with its method like:
Route::get('/your-url', 'App\Http\Controllers\UserController#addView');
Route::post('/your-url', 'App\Http\Controllers\UserController#store');
If you are making a CRUD you can have:
Route::resource('user', UserController::class);
This will create all the urls needed for a CRUD:
Actually you have same name for both routes i.e., name('user.add'). Change that.
Yes you can get issues in future. For example, I had issues after functionality update - I've updated route interface (added some fields) and new fields does not appeared in result after opening URL.
Please run command php artisan optimize to see is it all ok.
Been working on a neat little project. I'm using Laravel 5.5, and I'm building routes to handle the various requests. I've got a route that accepts a slug to locate a particular guild via route model binding. Works great! Beautifully, actually. Then, I defined a "static" route that doesn't use a parameter to show the form for creating a new guild. Here's the routes...
Route::get('/guilds', 'GuildController#index')->name('guilds');
Route::get('/guild/{guild}', 'GuildController#show')->name('guild');
Route::get('/guild/create', 'GuildController#create')->name('create_guild');
Route::get('/guild/{guild}/edit', 'GuildController#edit')->name('edit_guild');
Route::post('/guild/create', 'GuildController#store')->name('store_guild');
But when I attempt to navigate to '/guild/create', I get a 404 because a guild with the slug "create" doesn't exist. How can I work around this particular issue?
Try put specific routes first:
Route::get('/guilds', 'GuildController#index')->name('guilds');
Route::get('/guild/create', 'GuildController#create')->name('create_guild');
Route::post('/guild/create', 'GuildController#store')->name('store_guild');
Route::get('/guild/{guild}', 'GuildController#show')->name('guild');
Route::get('/guild/{guild}/edit', 'GuildController#edit')->name('edit_guil');
In laravel 5.1, I was able to get the route path by route name, for example:
Defined Route:
Route::post('users/{user_id}/delete', 'UserController#delete')->name('user:delete');
In laravel 5.1, when I tried the below method, It gave the route without any error If I didn't pass any route parameter:
route('user:delete'); // Output: http://example.com/users/%7Buser_id%7D/delete
and then in javascript, I simply replaced %7Buser_id%7D with the user id dynamically. But laravel 5.3 is throwing error when accessing route by name that has parameters and I don't want to pass one, because the parameters are set dynamically from the javascript.
Is there any way I can access route pattern by route name like:
http://example.com/users/{user_id}/delete
Or
/users/{user_id}/delete
Thanks in advance.
You can give some route method some value, that will be then replaced in javascript. For example: route('user:delete', 'USER_ID'), then in javascript you will simply replace USER_ID.
or the better way, is to use package called "Laroute"
Hi I have a hyperlink from a page:
<h3>hitest</h3>
the route:
Route::get('hitest', function(){ return 'hitest message';});
There is an error:
No query results for model [App\User2].
hyper link is from a page with this url
/userpage/1
the 1 is a model object.
Shouldn't the hyperlink route to /hitest ?
Please see my other post: Strange behavior with routing and hypertext.
I'm new at web development. Is there configurations for routing? The app is hosted (not local).
As bytesarelife already mentioned, you can use the url()-function, like so:
{{ url('your/url/') }}
A better way in terms of maintainability would be to give your routes names, so you would not have to replace every url in every template once you want to change it. You can do so, by adding the name in your routes:
Route::get('hitest', function(){ return 'hitest message';})->name('getHittest');
And then you can use the route function in your view:
{{ route('getHittest') }}