Laravel resource route doesn't display show in controller - laravel

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

Related

Laravel: Resource Controller change parameter from ID to Slug

In my Laravel application I was using the normal routes, such as GET, POST, PUT and all with the various controllers. But as the application progresses, the routes/api.php file is turning out quite bulky. So, I am changing it with the "Resource Controller" syntax, for cleaner and reduced code.
However, earlier I was able to use the "token" or "slug" parameters in the url to fetch the data, for example: show/{token} & show($token) or show/{slug} & show($slug), but now whenever I try these or any other parameter I get 404 not found error. Therefore I am stuck with the default "id" parameter only.
Earlier:
Route::get('/categories', 'CategoryController#index');
Route::get('/categories/{token}', 'CategoryController#show');
Now:
Route::resource('/categories', 'CategoryController');
Is there any way to change the default "id" parameter to any other ..?
For Resource Routing the route parameter is the resource name, first argument, in singular form.
Route::resource('categories', 'CategoryController');
Will give you a route parameter of category. You can see this by running php artisan route:list and looking at the routes that are defined.
If you want to use a different parameter name for a resource you can also do that by overriding the parameter:
Route::resource('categories', 'CategoryController')->parameters([
'categories' => 'something',
]);
Now the route parameter used would be something.
This only has to do with the route parameter name, it has nothing to do with how any binding would be resolved, unless you had an Explicit Route Model Binding defined specifically for a parameter name.
If you are using Implicit Route Model Bindings you would define on your Model itself what field is used for the binding via the getRouteKeyName method. [In the next version of Laravel you will be able to define the field used in the route definition itself.]
Laravel 6.x Docs - Controllers - Resource Controllers - Naming Resource Parameters
Laravel 6.x Docs - Routing - Model Bindings - Implicit Route Model Binding getRouteKeyName
This can also be achieved by changing the customizing the key. In here slug refers to a column in the Category model.
Route::resource('categories', 'CategoryController')->parameters([
'categories' => 'categories:slug',
]);

"Missing required parameters for [Route: property.edit]" error when editing product

I am trying to edit a product, but my route is not working. Please let me know how to get this resource route working.
This is my web file:
Route::resource('product', 'ProductController')->except([
'store', 'update', 'destroy', 'edit'
]);
Here is my controller file:
public function edit($product)
{
$product=Product::find($product);
return view('admin.product.edit', compact($product));
}
Here is my view file:
<li>Edit</li>
When you try to edit a resource you should provide the id to the resource. So if you run php artisan route:list you will see that your product route for editing expects an argument, something like this:
'product/{product}/edit', in order to make it work, you should do the following:
<li>Edit</li>
or
<li>Edit</li>
Third option
<li>
Edit
</li>
Your named route is called property.edit and you shared the product route with us, so please edit your question and provide the details. But in any case you are missing the argument hence the error.
You are not passing parameter to your route. The Route expecting a parameter. lets say you need to edit the product but you have to know which product you want to edit for this you pass your product id or product slug with the help you fetch the record right.
There are you doing mistake. you passing nothing. just do something below
<li>Edit</li>
In the above code passing id as a parameter and in controller getting result by passing received id from route like below.
$product=Product::find($product);
return view('admin.product.edit', compact($product));
If you want to see you route list and methods and arguments of every route required
you can run this command in terminal php artisan route:list

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 to declare routes with resources in Laravel 5.2

I have some routes in routes.php in laravel
// Code for rounting admin panel
Route::resource('/admin','Admin\LoginController#index');
Route::resource('/admin/dashboard','Admin\AdminController#index');
Route::resource('/admin/movies','Admin\MovieController#index');
Now when I access url http://localhost/askspidy/admin I want to show login page and it works, but when i access url http://localhost/askspidy/admin/dashboard it should go to dashboard but it's showing me login page only.
I know this is because when it found /admin in any url it's bydefault goes to the route
Route::resource('/admin','Admin\LoginController#index');
I know it's assuming that (/admin) is route to controller and (/dashboard) is the function declared in the controller but I want routing like this only so is there any other solution for this problem.
A RESTful Resource Controller takes over the responsibility of each action. You only need to list the name and the controller:
Route::resource('photo', 'PhotoController');
If you wanted to only use the index method, you’d list it like this:
Route::resource('photo', 'PhotoController', ['only' => [
'index'
]]);
However, it looks like two of your routes are not suitable for resources (login and dashboard), as they should relate to editing a model.
You should instead just use a get() resource instead.
From the docs:
Route::get('user/{id}', 'UserController#showProfile');
So in your case, it would be:
Route::get('/admin','Admin\LoginController#index');
Route::get('/admin/dashboard','Admin\AdminController#index');
Route::resource('/admin/movie','Admin\MovieController');

Laravel-4: Difference between RESTful Controllers and Resource Controllers in Laravel

Someone can please explain what is the difference between RESTful Controllers and Resource Controllers in Laravel ? I also have some Questions-
when should I use RESTful Controllers and when Resource Controllers?
Is there any naming convention Of Controller action for RESTful Controllers and Resource Controllers ?
If I use RESTful Controllers how could I define route for our controller ?
For building API which Controller Method is the best ?
Laravel Resource Controllers are defined as Route::controller('users', 'UserController'); while Restful Controllers are defined as Route::resource('photo', 'PhotoController');.
A restful controller follows the standard blueprint for a restful resource which mainly consists of:
GET /resource index resource.index
GET /resource/create create resource.create
POST /resource store resource.store
GET /resource/{resource} show resource.show
GET /resource/{resource}/edit edit resource.edit
PUT/PATCH /resource/{resource} update resource.update
DELETE /resource/{resource} destroy resource.destroy
While the resource controller isn't opinionated like the restful controller. It allows you to create methods directly from you controller and it all gets automatically mapped to your routes:
public function getIndex()
{
// Route::get('/', 'Controller#getIndex');
}
public function postProfile()
{
// Route::post('/profile', 'Controller#postProfile');
}
Will automatically have the routes like Route::post('/profile', 'Controller#postProfile'); without explicitly defining it on the routes, much more of a helper if you will to avoid very long route files.
Doing php artisan routes will show you all your routes. You can test stuff out and use that command to see what routes gets automatically generated.
They are different concepts. In laravel, a resource controller defines all the default routes for a given named resource to follow REST principles.
So when you define a resource in your routes.php like:
Route::resource('users', 'UsersController');
The only thing Laravel does is define for you this routes:
Verb Path Action Route Name
GET /resource index resource.index
GET /resource/create create resource.create
POST /resource store resource.store
GET /resource/{resource} show resource.show
GET /resource/{resource}/edit edit resource.edit
PUT/PATCH /resource/{resource} update resource.update
DELETE /resource/{resource} destroy resource.destroy
And expects that you define those methods on your controller. You can also use only/except clauses to remove unneeded routes:
Route::resource('user', 'UserController', ['except' => ['destroy']]);
More on this on Laravel's documentation.
It's just a distinction about the routing declaration. Instead of using one of those, manually define all of your routes.
Route::get(...);
Route::post(...);
Route::put(...);
Route::delete(...);
Route::patch(...);
It makes your routes file authoritative, easy to understand, and less buggy.
The documentation currently shows RESTful and Resource controllers to refer to the same thing.
Route::resource('resource', 'ResourceController');
It defines the routes for the following http request verbs mapped to the URI, controller action, and route. This allows you to use the predefined route names to connect to predefined controller actions and will map resource_id to {resource} as shown.
Verb URI Action Route Name
GET /resource/index.blade.php index resource
GET /resource/create.blade.php create resource.create
POST /resource store resource.store
GET /resource/{resource}/show.blade.php show resource.show
GET /resource/{resource}/edit.blade.php edit resource.edit
PUT/PATCH update resource.update
DELETE destroy resource.destroy
The term Implicit Controller seems to be the term to specify the use of
Route::controller('resource', 'ResourceController');
which will magically connect all routes to to ResourceController so that the http request verb (get/post) is prefixed in the function name used in the controller. This maps any URI to the controller action (function) with (get/put) in front but does not map resource_id to {resource} or route names.
class UserController extends BaseController {
public function getIndex()
{
//
}
public function postProfile()
{
//
}
public function anyLogin()
{
//
}
}
maps to
Verb URI Action Route Name
GET /index getIndex
POST /profile postProfile
GET /login anyLogin
POST /login anyLogin
DELETE /login anyLogin
It's up to you to decide which method to use if any for routing. There is some debate as to what is useful and if routing is even worth the confusion it can cause.
RESTful Resource Controllers
Resource controllers make it easier to build RESTful controllers around resources. For example, you may wish to create a controller that manages "photos" stored by your application. Using the controller:make command via the Artisan CLI and the Route::resource method, we can quickly create such a controller.
To create the controller via the command line, execute the following command:
php artisan controller:make PhotoController
Now we can register a resourceful route to the controller:
Route::resource('photo', 'PhotoController');
This single route declaration creates multiple routes to handle a variety of RESTful actions on the photo resource. Likewise, the generated controller will already have stubbed methods for each of these actions with notes informing you which URIs and verbs they handle.
Actions Handled By Resource Controller
http://laravel.com/docs/5.0/controllers#restful-resource-controllers

Resources