Why is my show function not working in Laravel 5.8 - php

Below is the code in my route
Route::get('patients/{$patient}/','PatientController#show');
The above will not work in my browser , it will say 404 | not found with the url
below http://127.0.0.1:8000/patients/3
But
if I change my route to code below
Route::get('patients','PatientController#show');
I will not get any error , infact it will display the view for me.
What could be the problem? I am using Laravel 5.8

You have a typo in your route, {$patient} should be {patient}.
Route::get('patients/{patient}/','PatientController#show');
Then using model-route-binding, you will have the $patient model ready for you in your show() method,
public function show(Patient $patient){
return view('patients.show', ['patient' => '$patient']);
}

This is your route problem.
Route::get('/patients/{patient?}', 'PatientController#show');
You can use url like this
http://127.0.0.1:8000/patients/3
or
http://127.0.0.1:8000/patients
And your controller is also simple
public function show(Patient $patient=null){
return view('patients.show',compact('patient'));
}

Related

All routes working fine but the new created route return 404 in laravel

Am trying to add a route in my api.php in laravel, whereas all other api are working fine but newly added api is not working returning 404.
Route::get('products','Api\ProductsController#index');
Route::get('products/trending','Api\ProductsController#trendingProducts');
Products return data but when searched for trending it returns 404. Tried clearing route cache and checking in route list. The route exist in routelist. Even the function in controller also existl.
Controller Code :
public function trendingProducts(){
echo "i";
}
Confused by the unexpected behaviour. Any help will be gratefull. Thank you in advance.
You have to use like this
href="{{url('')}}/api/products/trending"

Custom 404 laravel

If we just create a 404.blade.php page in resources/views/error it will work fine, but Auth() won't work on 404 page, to solve that if we follow the solution available on stackoverflow the Laravel auth errors will stop working. I use the following solution to do the work.
Create custom view
resources/views/errors/404.blade.php
in route.php
Route::any('{catchall}', 'PageController#notfound')->where('catchall', '.*');
create PageController and add this function
public function notfound()
{
return view('errors.404');
}
For Laravel 5.6 and later, you can use fallback in your routes\web.php:
Route::fallback('MyController#show404');
It works as an "catch all"-route.
See docs here.
Write below code in your Exceptions/Handler.php
if($this->isHttpException($exception)){
if(view()->exists('errors.'.$exception->getStatusCode())){
$code = array('status'=>$exception->getStatusCode());
return response()->view('errors.404',compact('code'));
}
}
Now create a new file in your view i.e errors/404;
Now in code array you can pass dynamic values
Not sure if this will help. Laravel has a PHP artisan command to publish error pages. Laravel Custom HTTP Error Pages
After you run this artisan command use Route:fallback() method as #KFoobar suggested. If you use a closure function, no need to use a controller. Make sure to add the below route at the ends of your routes file.
//Fallback/Catchall Route
Route::fallback(function () {
return view('errors.layout');
});
Shameless plug for my BLOG

Unable to access laravel 5.5 API route using Postman software

My api.php route is
Route::get('/allposts','PostController#index');
Controller function is and working with web.php Route file
public function index()
{
$posts= Post::all();
return PostResource::collection($posts);
}
my Resource is toArray function is
public function toArray($request)
{
return parent::toArray($request);
}
Postman link using GET are
"queuetest.com/api/allposts"
"http://queuetest.com/api/allposts"
both are not working
and getting Result in both Post man and browser: Sorry the page your looking for could not be found
Do you get something other than the Sorry the page you're looking for could not be found message when you do var_dump('test');die(); in your index method as the first line.
Edit: try to remove then / before /allPosts
Another edit: check in your RouteServiceProvider if the mapApiRoutes prefix is set to 'api'
If you are using the default route configuration that comes out of the box, you should place your route in routes/api.php if you want to access it in the /api namespace. Right now, the route should be accessible from http://queuetest.com/allposts.
This behavior can be configured in App\Providers\RouteServiceProviders.php. If you have a modified route configuration, there are numerous things that could cause this behaviour, and it is impossible to locate the problem without seeing more code.

Laravel 5.4 function index try using function show

I have route:
Route::resource('admin/question', 'QuestionsController');
and function index:
public function index() {
return "Hello";
}
But when I try used index Laravel returned me the error:
Method [show] does not exist.
I'm using the link:
http://localhost:8012/siwz/siwz/public/admin/question
The server is WampServer program.
I can only use index function when I change route file:
Route::get('admin/question/index', 'QuestionsController#index');
Route::resource('admin/question', 'QuestionsController');
In Laravel version 5.3 I did not have to do it, it was enough to use:
Route::resource('.../...', '...Controller');
Actually, the URL is going to the correct function. admin/question should go to index. admin/question/{question} is the route that goes to show.
Take a look here and check how Laravel create Resource routes:
https://laravel.com/docs/5.4/controllers#resource-controllers
Since that you didn't provide your full routes. I am guessing that the link you were accessing is going to the wrong controller. You should check the ordering of the routes. Maybe you are accessing something like this on your route,
Route::resource('admin','AdminController');
And the AdminController doesn't have a method show(). Thats why laravel return that error.
Here's you can do
Comment out the rest of routes except route that you are accessing.
Try to reorder the affected routes. Maybe Laravel is confused with your route.

laravel giving incomprehensible(to me) http exception

I use laravel 4 and am not able to show a component :
this is a line from my routes
Route::resource('/', 'PostsController');
and this is my show function from PostsController.php
public function show($id) {
return "HI";
}
And This is the line that links to the function from my view
<h1>{{$post['title']}}</h1>
And It properly links to localhost:8000/show/1
But I'm amazingly getting a Not found HTTP exception from laravel.
How do I get this to work?
So the answer is that #NihalSahu is wrong (me) and that resourceful routing doesn't work like that it would be infinitely better so as to set the router to host/{id}

Categories