NotFoundHttpException No query results for model [App\Article] - laravel

I'm having a strange problem. For some reason, I am getting the following error when I try to go to this route: article/create. The complete error is this:
(2/2) NotFoundHttpException
No query results for model [App\Article].
This is my route:
Route::get('/article/create','ArticlesController#create');
And this is my controller:
public function create()
{
return view('articles.create');
}
All standard things, I don't know why this is happening.

The exception NotFoundHttpException means that the route you are trying to access is not registered within your routes/web.phpfile.
You have a registered route as
Route::get('/article/create','ArticlesController#create');
You might be accessing this route from within your blade file on a form submission. The possible reason is that you might be using method=POSTon your form while the route registered is GETwhich is causing the problem.
If this is the reason one solution is to register your route as POST
Route::post('/article/create','ArticlesController#create');

Related

Get Controller name and Method in laravel

I have to get my controller and method name from route, for example:
Route::get('/', 'HomeController#index')->name('home');
I have to get:
HomeController#index
Also, I have to get name of the action of my route(like POST and GET) automatically from my controller as well.
EDIT:
https://www.nicesnippets.com/blog/how-to-get-current-route-name-path-and-action-in-laravel-6
Thanks to this website, now I can get the method and controller from routes, the remaining thing that I still dont found is to how to get the action(ex:POST or GET) from my route
Does anyone have any suggestions?
You are looking for request()->route()->getActionName();

Laravel resource route doesn't display show in controller

According to the Laravel documentation:
This single route declaration creates multiple routes to handle a
variety of actions on the resource. The generated controller will
already have methods stubbed for each of these actions, including
notes informing you of the HTTP verbs and URIs they handle.
In my route I have this:
Route::resource('admin/companies', 'CompaniesController');
In my controller I have index, create, store, show etc.
Specifically in show I have this:
public function show(Company $company)
{
//
dd('hi');
}
I would expect when I hit this route:
http://127.0.0.1:8000/admin/companies/onecompany
for it to dd my response. Instead I get a 404 error. What am I doing wrong?
This route http://127.0.0.1:8000/admin/companies/onecompany is not a valid route that triggers resource functions according to Laravel documentaion.
The right URL that will trigger the show(Company $company) function is:
//This route will extract the `id` column from the model and show the required record.
http://127.0.0.1:8000/admin/companies/{company}
or
http://127.0.0.1:8000/admin/companies/{id}
Try an existing record in your database;
//Assuming there is a record with an id of 1
http://127.0.0.1:8000/admin/companies/1
cause of your problem seems related to Route model binding
,try it with id in uri.
to see list of your application's routes: php artisan route:list

Same route on first link it works, but on second not. Laravel 5.6

Route file web.php:
Route::get('/download/received/{image_id}/{isoriginal?}', 'DownloadController#download_recv_image');
View:
<li>Download {{strtoupper($image->extension)}}</li>
<li>Download PNG</li>
Function in controller:
public function download_recv_image($image_id, $original=false){...}
This is function for download received image. When I click on first link in view route is called and function is executed. But on second link where I'am not sending second parameter then it returns me error 404 and it looks like it cant catch route.
(I have another function for download user images, with same logic for route definition in another two links and there everything works.)
I have found where the problem is.
That's because above that route I have another route called:
Route::get('download/{image_id}/{isoriginal?}', 'DownloadController#download_user_image');
I have changed second route to /received/download instead of /download/received
It's messing up because both routes have the same beginning and parameters ar messed up.

laravel api route is not redirecting?

I am new to laravel,I had wrote api route code to register controller:
Route::post('test','Api\Auth\RegisterController#index');
In Register controller i had written simple code
public function index(Request $request)
{
return 'hello';
}
I am getting the output in postman like:
Sorry, the page you are looking for could not be found.
not hello.
Here the images:
1 3
Routes defined in the routes/api.php file are nested within a route
group by the RouteServiceProvider. Within this group, the /api URI
prefix is automatically applied so you do not need to manually apply
it to every route in the file.
You are trying to make a request to a route which does not exist.
In Postman
Change:
http://localhost:8080/App/api/test
To:
http://localhost:8080/api/test
You're telling laravel to route assoaciate '/api/test', not '/App/api/test', which is the adress you're trying to reach.
Also, if you plan to reach that address straight from the location bar of your browser, you should register the 'GET' method as well.
As you are returning something in function you need to use route as get() instead of post().
Route::get('test','Api\Auth\RegisterController#index');

How do I declare a controller method for a named route in Laravel 4?

I am attempting to declare a method/function in my controller that responds to a numerically named route. When I load any page in the site I receive an error stating the controller method could not be found, meaning that Laravel won't even load the application do to some incorrect formatting. I searched for an answer with no luck.
Here is the route I'm attempting to access via my Math controller:
students/academics/math/7-12
Here is the method declaration to look for the route:
public function get712()
Which gives me the following error no matter what page I'm loading:
Call to undefined method Illuminate\Routing\Router::get712()
I'm not sure how to name the function/method in my controller for a purely numeric routes since hyphen is not allowed and there is no upper/lowercase for numbers.
why not pass 7-12 as a variable to a method?
route:
students/academics/math/{number}
controller:
public function getMath($number)
{
// code here
}
I remembered that Laravel used to use underscores in the method name instead of camelCase so after scouring Google with no luck I declared the method like this:
public function get_7_12()
voila!

Resources