Suppose I have these routes :
$api->group(['prefix' => 'Course'], function ($api) {
$api->group(['prefix' => '/{course}'], function ($api) {
$api->post('/', ['uses' => 'CourseController#course_details']);
$api->post('Register', ['uses' => 'CourseController#course_register']);
$api->post('Lessons', ['uses' => 'CourseController#course_lessons']);
});
});
As you can see all / , Register and Lessons route prefixed by a course required parameter.
course parameter is a ID of a Course model that I want to use for route model binding.
But In the other hand when I want use course parameter for example in course_details function, it returns null. like this :
public function course_details (\App\Course $course)
{
dd($course);
}
But if I use below, all things worked fine :
public function course_details ($course)
{
$course = Course::findOrFail($course);
return $course;
}
Seems that it can not bind model properly.
What is Problem ?
Update :
In fact I'm using dingo-api laravel package to create an API. all routes defined based it's configuration.
But there is an issue about route model binding where to support route model binding we must to add a middleware named binding to each route that need model binding. HERE is described it.
A bigger problem that exists is when I want to add binding middleware to a route group, it does not work and I must add it to each of routes.
In this case I do not know how can I solve the problem.
Solution:
After many Googling I found that :
I found that must to add bindings middleware in the same route group that added auth.api middleware instead adding it to each sub routes separately.
means like this :
$api->group(['middleware' => 'api.auth|bindings'], function ($api) {
});
add in kernel.php
use Illuminate\Routing\Middleware\SubstituteBindings;
protected $routeMiddleware = [
...
'bindings' => SubstituteBindings::class,
];
and in your group route:
Route::middleware(['auth:sanctum', 'bindings'])->group(function(){
... you routes here ...
});
this worked for me. thanks
As you said
course parameter is a ID of a Course
You can use Request to get id, try like this
public function course_details (Request $request)
{
return dd($request->course);
}
I came across a similar issue. I think you need to use the 'bindings' middleware on your routes.
See my answer here:
https://stackoverflow.com/a/55949930/2867894
Take a close look on:
// Here $course is the id of the Course
public function course_details ($course)
{
$course = Course::findOrFail($course);
return $course;
}
But here:
// Here $course is the object of the model \App\Course
public function course_details (\App\Course $course)
{
dd($course);
}
that should be
public function course_details ($course, \App\Course $_course)
{
// add your model here with object $_course
// now $course return the id in your route
dd($course);
}
After searching for 2 hours I got the issue details
For route binding to work, your type-hinted variable name must match the route placeholder name
For example my edit method
Here is my route URI for the edit
user/role/{role}/edit
As you can see there is {role} placeholder in the route definition, so the corresponding variable must be called $role.
public function edit(Role $role)
{
return view('role.edit',compact('role'));
}
Related
I have a route like the following.
Route::get('/articles/{articleSlug}' ,
[App\Http\Controllers\ArticleController::class, 'single']);
And the method of single() at ArticleController class goes here:
public function single($slug)
{
$article = Article::where('slug',$slug)->first();
$article->increment('viewCount');
return view('home.article',compact('article'));
}
Now I wish to use Route Model Binding for finding this data from the articles table based on the column slug. But as I know, Route Model Binding finds data based on the id. So how to change Route Model Binding finding data from id to slug ONLY for ArticleController.php (meaning that the other Controller classes can work with id as route model binding)?
In case you want to use other model field as the biding attribute instead of id you can define a getRouteKeyName which return the name of the field which must be use
class Article extends Model {
// other methods goes here
public function getRouteKeyName() {
return 'slug';
}
}
Or you can pass the field name directly when you define the route like this
Route::get('/articles/{article:slug}' , [App\Http\Controllers\ArticleController::class, 'single']);
With this code inside of your controller you must ensure that the name provide as parameter in the route definition match the name of the controller argument
public function single(Article $article)
{
$article->increment('viewCount');
return view('home.article',compact('article'));
}
Your controller is already set up, all you need to do is change your variable name to $slug in the route, and I believe that should be enough:
Route::get('/articles/{slug}' , [App\Http\Controllers\ArticleController::class, 'single']);
change your route to this:
Route::get('/articles/{article:slug}' , [App\Http\Controllers\ArticleController::class, 'single']);
and then inject the Article model to your controller function and let laravel do the rest for you:
public function single(Article $article)
{
$article->increment('viewCount');
return view('home.article',compact('article'));
}
you can customize route model bindings directly in the route definition:
past given code in app/model/Article.php:
public function getRouteKeyName()
{
return 'slug';
}
2.when you use slug change route to
Route::get('/articles/{article:slug}' , [App\Http\Controllers\ArticleController::class, 'single']);
to use id sample change slug to id
Route::get('/articles/{article:id}' , [App\Http\Controllers\ArticleController::class, 'single']);
you can add bind method to your model boot() like this
public function boot()
{
Route::bind('article', function ($value) {
return Article::where('slug', $value)->firstOrFail();
});
}
to learn more about it read this section in the Laravel docs
https://laravel.com/docs/9.x/routing#customizing-the-resolution-logic
I am getting a "404 | Not Found" error when i try to access a specific item from my database. The items with the specific ID's do exist in the database, but i am not even sure if that even has any influence on the problem.
My routing looks likes this:
Route::prefix('departments')->group(function() {
Route::get('/{id}', [DepartmentController::class, 'showDepartment']);
});
And the related controller looks like this:
public function showDepartment() {
return '';
}
}
I haven't yet finished the function. I just wanted to check if the routing even worked by returning an empty string.
So what am i doing wrong? Is it the routing or the controller?
According to the Laravel documentation, you have to define the parameter in the route then use it in the controller as a parameter.
in your controller:
public function showDepartment($id) {
return 'Hello';
}
The id is not bound to any model to fetch from the database to do that you can use Laravel route model binding
for example when you have a model named Department you write your route like this:
Route::get('/{department}', [DepartmentController::class, 'showDepartment']);
and in the controller:
public function showDepartment(Department $department) {
return 'Hello from depratment';
}
When your department exists it returns the response otherwise return 404.
You may need a Department model class. Then you can find the item from database by id $department = Department::findOrFail($id);
you are send parameter in route and this function without any parameter
route :
Route::prefix('departments')->group(function() {
Route::get('/{id}', [DepartmentController::class, 'showDepartment']);
});
your function in controller should be
public function showDepartment($id) {
$department = Department::findOrFail($id);
}
}
I have this:
Route::get('/admin/funcionarios', [App\Http\Controllers\AuthController::class, 'funcionarios'])->name('funcionarios');
and this:
<?php
namespace App\Http\Controllers;
use App\Models\Funcionarios;
class FuncionarioController extends Controller {
public function destroy($id) {
$funcionario = Funcionarios::find($id);
$funcionario->delete();
return redirect()->route('admin.funcionarios');
}
}
The problem is I get this error: Route [admin.funcionarios] not defined. (Symfony\Component\Routing\Exception\RouteNotFoundException)
Any ideas how to solve?
You can name your route what you would like, so instead of funcionarios you could name it admin.funcionarios if you prefer:
Route::get(...)->name('admin.funcionarios');
You can also use a Route Group to cascade this admin. name to the routes inside of it if you want:
Route::name('admin.')->group(function () {
Route::get(...)->name('funcionarios');
});
Now that route name for that route would be admin.funcionarios as route groups "cascade" configuration.
Laravel 8.x Docs - Routing - Named Routes
Laravel 8.x Docs - Routing - Route Groups - Route Name Prefixes
Try this:
Route::get('/admin/funcionarios',['as' => 'admin.funcionarios', App\Http\Controllers\AuthController::class, 'funcionarios']);
If you want to return redirect()->route('admin.funcionarios');
Try to change :
Route::get('/admin/funcionarios', [App\Http\Controllers\AuthController::class, 'funcionarios'])->name('funcionarios');
To
Route::get('/admin/funcionarios', [App\Http\Controllers\AuthController::class, 'funcionarios'])->name('admin.funcionarios');
or you can do it like this:
public function destroy($id) {
$funcionario = Funcionarios::find($id);
$funcionario->delete();
return redirect()->route('funcionarios');
}
if what the name part of the route is, you must write it in parentheses.
I have this route:
Route::get('{country}/{federalstate?}/{city?}', ['as' => 'regions.show', 'uses' => 'RegionsController#show']);
Next I have a model Country with a few countries in it.
I want this route only to take effect, when given {country} exists in the model Country. I want to do this, because don't wan to use a prefix for that route.
The ugly way would be a regular expression. Ugly, because I would have to update it every time I add a new country to the model.
So what's the best way to accomplish what I want to do?
Maybe this could help -> https://laravel.com/docs/5.4/routing#route-model-binding - but I am a beginner, and I can't get it to work.
edit:
this might work:
Route::get('{country?}/{federalstate?}/{city?}', ['as' => 'regions.show', 'uses' => 'RegionsController#show'])->where([
'country' => 'germany|usa|canada'
]);
But, as I said, I would have to change the Regex every time I add a new country to the database.
Here's my solution for now:
in AppServiceProvider in the boot() method:
$available_countries = Country::select('slug')->get();
$available_countries = $available_countries->toArray();
$tmp = array();
foreach ($available_countries as $country)
{
$tmp[] = $country['slug'];
}
Config::set('app.available_countries', $tmp);
and in my routes file:
Route::get('{country?}/{federalstate?}/{city?}', ['as' => 'regions', 'uses' => 'RegionsController#index'])->where([
'country' => implode('|', config('app.available_countries'))
]);
As you suggested, Route-Model binding may be your best bet. You can see from the docs that if the model you've bound to your route does not exist, the route will return a 404.
Just be aware that the default functionality in Route-Model binding expects an ID from the URL. If you want to override that, you can do the following:
/**
* Get the route key for the model.
*
* #return string
*/
public function getRouteKeyName()
{
return 'name';
}
In my opinion, you could pass Country to your controller as model. If there is no such model in your DB, Laravel will produce 404 error.
I am having issues with Laravel 4. When a user logs in, they are able to create or edit a saved form called a "Project". I need to be able to make it where a user can only edit the forms "projects" that they created. Currently, anyone can edit anyone elses project by changing the project id in the url.
Example: projects/3/edit edits the project with an id of 3
I tried using the following code but it returns an error "Trying to get property of non-object" but project is an object. Any advice is much appreciated.
filter.php
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::guest('login');
});
Route::filter('auth.project', function($route, $request)
{
if($route->parameter('project')->user_id !== Auth::user()->id)
{
return Redirect::route('/')->with('error','sorry, you can only access projects that you created');
}
return Redirect::route('/');
});
Route::filter('auth.basic', function()
{
return Auth::basic();
});
routes.php
Route::get('/projects/{projects}/edit','ProjectsController#edit', ['before' => 'auth.project']);
You have this filter:
Route::filter('auth.project', function($route, $request)
{
if($route->parameter('project')->user_id !== Auth::user()->id)
{
return Redirect::route('/')->with('error','sorry, you can only access projects that you created');
}
return Redirect::route('/');
});
Here if($route->parameter('project')->user_id !== Auth::user()->id) is not right because the first thing is that, your route is declared as given below:
// This is not right
Route::get('/projects/{projects}/edit','ProjectsController#edit', ['before' => 'auth.project']);
Try this:
Route::get('/projects/{projects}/edit', ['before' => 'auth.project', 'uses' => 'AuthorsController#edit']);
Also, the parameter is projects not project but you are using project in your filter and then, the second problem is that, you are trying to get property of non-object because $route->parameter('project') returns null and even if you use projects then it may return the 3 but still that will throw an error because 3 is not an object. You need to grab that Project whose id is 3 or you may use a route model binding. So, if you use something like this:
$project = Project::find($route->parameter('projects')); // Assumed that 3 is id
if($project->user_id !== Auth::user()->id) {
//...
}
Also you may use a route model binding like this:
Route::model('projects', 'Project'); // Project Model must be existed
Route::get('/projects/{projects}/edit', ['before' => 'auth.project', 'uses' => 'AuthorsController#edit']);
Then in your edit method:
public function edit(Project $project)
{
// $project is an Eloquent Object, will be injected automatically...
});
Well, just looking at what you've posted here, I see a couple issues.
First, you named your parameter projects in your route and try to access project in your filter, which is non-existent.
Second, $route is of type Illuminate\Routing\Route which has a method called getParameter, not parameter. More can be found in the documentation linked here.
Hope this helps.