laravel 6 route returing 404 not found - php

While Passing variable in URL and route returning only 404, not found#.
Did some google and found some result and tried some methods on youtube its also not working. Totally Beginner to laravel
room.blade.php
//In roomdetail->id its passing the value of room
Detail
web.php
//The route which I am using
Route::get('/detail/{$id}', 'DetailController#show')->name('detail.show');
DetailController.php
public function show($id)
{
dd($id);
}
}
In DD I want to get the Id so I can fetch data from the database it's showing only 404

Your route is wrong you are using $ in a param which is wrong instead of {$id} in route it should be {id}
change this
Route::get('/detail/{$id}', 'DetailController#show')->name('detail.show');
To
Route::get('/detail/{id}', 'DetailController#show')->name('detail.show');
and a quick tip as you are using named route then it would be better if you use route method to pass variable to route in view
So in your view change this:
Detail
To
Detail
Thanks

Remove '/' from URL or pass route
Detail
OR
Detail
or change your route as {id} instead of {$id}
it should be Route::get('/detail/{id}', 'DetailController#show')->name('detail.show');

Related

Why does my edit & delete methods return a "404 not found" when my index & create routes work

Laravel version has updated and the routes is now expecting an object instead of an id from when i last used it.
My Routes:
When I try to pass over the $item object which the method in the controller wants. I get a 404 not found and my logs aren't returning... meaning the function isn't running. When the $item obj is not passed over the function realizes that a parameter is missing thus the method is recognized by the blade as being the same as the one in the controller.
Calling the edit function in Blade:
Controller Code:
I appreciate any help whatsoever.
The order of your routes is probably wrong
when you first define the show route with /item/{item} and then create with /item/create laravel will think the "create" is the id (or reference)
best way is to have
the index
create
....
show
Code Example correct
Route::get('/', ProductIndex::class)->name('product.index');
Route::get('/new', ProductCreate::class)->name('product.create');
Route::get('/{product}', ProductShow::class)->name('product.show');
Code Example wrong
Route::get('/', ProductIndex::class)->name('product.index');
Route::get('/{product}', ProductShow::class)->name('product.show');
Route::get('/new', ProductCreate::class)->name('product.create');

Laravel route not working for get request with parameters

This is my url :
http://localhost:8000/password-reset?token=$2y$10$N8AX4N9XMhP4NKPCK6NtjOvwoOzeWmaQnOJkxrKg4Ul7shNhp0zdu
This is how my route looks like:
Route::get('password-reset?token={token}', 'Auth\PasswordResetController#index');
However this is not getting captured in the controller.
Change the route first part to password-reset (remove ?token={token}) and then in controller use request('token') to get the token value.

Laravel. conflict with routes

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.

laravel 5 slug will not return pages

I try to get my route without any additional parameter in url and i'm getting Sorry, the page you are looking for could not be found. error.
If I use url like: domain/category/lenovo where lenovo is my slug it's working. But if I try to get my url like: domain/lenovo I'm gettin error above.
here is my codes:
route
Route::get('/{categoryslug}', 'frontend\FrontendController#totalcategoriessubs')->name('catwithsubs')->where('categoryslug', '[\w\d\-\_]+');
function
public function totalcategoriessubs($categoryslug) {
$categories = Category::where('slug','=',$categoryslug)->with('subcategories')->paginate(12);
return view('front.categoriessubs', compact('categories'));
}
it also brakes my other urls such as domain/admin/dashboard etc.
any idea?
It seems that is caused by the order of your routes.
Your actual problem is that when you hit a fixed endpoint, for example /dashboard this will be handled by Route::get('/{categoryslug}', ...) before the correct endpoint (Route::get('dashboard', ...)). So, as your system notices that there is no category with that slug (dashboard) it throws the error.
Try change the order of your routes, like this:
Route::get('/category/{categorySlug}', 'CategoryController#index');
Route::get('/dashboard', 'DashboardController#home');
// and so on..
Route::get('/{categoryslug}', 'frontend\FrontendController#totalcategoriessubs')
->name('catwithsubs')->where('categoryslug', '[\w\d\-\_]+');
Always put the routes that have fixed parts (.../category/...)of the route before the ones that have dynamic elements (.../{categoryslug}/).

Dynamic routes doesn't work in Laravel 5.5

I have some problem with dynamic routes.
I just tried add likes value in posts table to some post.
web.php
Route::get('post/{$id}/like', 'PostController#like')->name('post.like');
Route::resource('post', 'PostController');
And i define 1 custom method in resource controller PostControleler.
PostController#like
public function like($id)
{
$post = Post::find($id);
$post->likes++;
$post->save();
return redirect()->back();
}
and link in blade php view
Like
When clicking on a link, nothing happen, just displayed 404
Sorry, the page you are looking for could not be found.
Why that dynamic route doesn't work.
P.S.
That code works if i replace method to show instead like (it means that the custom method is hindered by something, the code itself is working)
Thanks in advance.
you need to replace your href by this below and make shure if $post it's $post of id value or replace it with $post->id
Like
and change get by post (here your are send data to the server that means you will post $id)
Route::post('/post/{$id}/like', 'PostController#like')->name('post.like');
Damn....i just forgot delete $ sign in custom route.
Should be:
Route::get('post/{post}/like', 'PostController#like')->name('post.like');

Categories