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');
Related
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');
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 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}/).
I'm trying to do something very simple: When a user clicks a button, a column for a corresponding row is changed from 0 to 1.
The button goes to:
href="/redeem-coupon/{{ $coupon->coupon_code }}"
That route is set up as follows:
Route::post('/redeem-coupon/{coupon_code}', 'CouponController#redeemCoupon');
And the controller is:
public function redeemCoupon ($coupon_code)
{
$coupon = \DB::table('coupons')->where('coupon_code', $coupon_code)->first();
$coupon->redeemed = 1;
return redirect('/');
}
I have some existing code that uses a route to display some info from a row when the rows 'coupon_code' is working so everything else is working fine except for this part. Clicking the button gives
"Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException "
You are doing GET request while route is set for POST. Either change your route
Route::get(....
or switch to use HTML form and POST submission.
Change your route to intercept the get request instead of post:
Route::get('/redeem-coupon/{coupon_code}', 'CouponController#redeemCoupon');
EDIT:
The error MethodNotAllowedHttpException means that you are trying to access a non existing route, but sometimes you will get this error even if the route is setting correctly, in this case make sure that your route is above all other routes pointing to the same controller because some other routes may intercept your query.
I have a very weird problem. I could access the page and everything was fine until i added a few new routes in my web.php routing file. Problem is with 5th route(named post.create). The ** are just to highlight the line/route i am talking about:
Route::group(['prefix'=>'admin', 'middleware'=>'auth'], function()
{
Route::get('home', 'HomeController#index')->name('admin.home');
Route::get('post/all','PostsController#index')->name("post.all");
Route::get('post/{id?}','PostsController#show')->name('post.fetch');
**Route::get('post/create','PostsController#create')->name('post.create');**
Route::post('post/store', 'PostsController#store')->name('post.store');
Route::put('post/{id?}','PostsController#update')->name('post.update');
Route::delete('post/delete/{id}','PostsController#destroy')->name('post.delete');
Route::get('category/create','CategoriesController#create')->name('category.create');
Route::post('category/store','CategoriesController#store')->name('category.store');
Route::get('category/all','CategoriesController#index')->name('category.all');
Route::get('category/{id?}','CategoriesController#show')->name('category.fetch');
Route::delete('category/delete/{id}','CategoriesController#destroy')->name('category.delete');
Route::put('category/{id}','CategoriesController#update')->name('category.update');
});
When i am accessing this route i get a blank page with a pair of curly braces only, nothing else. There is a message on the browser console that says - Resource interpreted as Document but transferred with MIME type application/json.
But if i change the route to
Route::get('posts/create','PostsController#create')->name('post.create');
,which is just add an additional s, i get the full view of the page.
I cannot seem to figure out why the earlier route is sending back application/json(seems an empty object). I made no change to the controller function. Here is the code for the PostsController#create function:
public function create()
{
$categories = Category::all();
return view('admin.posts.create', compact('categories'));
}
I have tried to return a different view or a simple string from this function for this route. Nothing seems to work.
What am i doing wrong, can anyone please help?
Laravel will serve the first route matched in the order you define them. Since you have post.fetch first it is serving that route with 'create' as the id parameter.
In your routes file place post.create before post.fetch so you have:
Route::get('post/create','PostsController#create')->name('post.create');
Route::get('post/{id?}','PostsController#show')->name('post.fetch');
Route::post('post/store', 'PostsController#store')->name('post.store');
Route::put('post/{id?}','PostsController#update')->name('post.update');
You should name Blade file as:
resources/views/admin/posts/create.blade.php
Blade view files use the .blade.php file extension and are typically stored in the resources/views directory
https://laravel.com/docs/5.4/blade#introduction
Update
In comments I've recommended you to move the route before the 'post/{id?}'.