route model binding with query strings - php

If I have a url like this:
localhost:8000?campaign=1
Is it possible to fetch the campaign from db like what we do in route model binding

Instead of localhost:8000?campaign=1 you should pass query string as: localhost:8000/campaign/1
and in Route:
Route::get('campaign/{id}', ['as' => 'campaigns.index', 'uses' => 'CampaignController#index']);
and in Controller:
public function index($id)
{
//Your Code Goes Here
}
That's it! :)

Related

How to change Route Model Binding finding data from id to slug

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

how can I access to another column in blade

I have the id in a blade and I want to access to another column.
I have $article->country_id which gives access to the id of the foreign key country.
How can I find the record using id and then get access to country->name.
I have done the things below but it doesn't work as I have explained.
Here is the blade
#foreach($articles as $article)
<div class="col-md-12">
کشور:<h4 class ="post-title">{{route('article.whichCountry',$article->country_id)}}</h4>
</div>
#endforeach
here is web.php
Route::get('article/whichCountry/{country}',[
'uses' => 'ArticleController#whichCountry',
'as' => 'article.whichCountry'
]);
and here is whichCountry in ArticleController
public function whichCountry($id){
$country = country::where('id',$id)->first();
return $country->name;
}
The point is that it doesn't call the function whichCountry
and prints:
http://127.0.0.1:8000/article/whichCountry/10
Thank you very much for your response in advance!
Your route access needs to be like below, giving value for {country} parameter.
{{ route('article.whichCountry',['country' => $article->country_id]) }}
Second, instead of where in your controller method, if Id is the primary key, you can just do:
Country::find($country);
Also, change your function definition like below for the parameter value filling
public function whichCountry($country){
This is very simple to implement this. First check your Controller is getting the id properly. Then use find method
Your route should be like this
{{route('article.whichCountry',['id'=>$article->country_id])}}
And in your controller you have to use like this
public function whichCountry($id){
$country = country::where('id',$id)->first();
return $country->name;
}
In your web.php looks like this
Route::get('article/whichCountry/{id}',[
'uses' => 'ArticleController#whichCountry',
'as' => 'article.whichCountry'
]);

Route Model Binding does not work on route group in laravel

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'));
}

Laravel Route Model Binding: Check if a route parameter exists in model

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.

It is possible to use one route with two functions in Laravel 5.1?

Route::get('post/form/{id}', array('as' => 'admin.post.delete', 'uses' => "PostController#deleteForm"));
Route::get('post/form', array('as' => 'admin.post.create', 'uses' => "PostController#createForm"));
I want to combine two routes above in a route for two functions, create and delete. Because the both routes have only different id.
Route::get('post/form/{id}', array('as' => 'admin.post', 'uses' => "PostController#getForm"));
If I want to type without id, it redirects to create function. If I type with id, it redirect to delete function.
How can I use one route for two functions?
As mentioned by James this is not really practical but you could achieve this via the following.
Laravel gives you the possibility to define optional route parameters as shown below.
Route::get('user/{name?}', function ($name = null) {
return $name;
});
Route::get('user/{name?}', function ($name = 'John') {
return $name;
});
Laravel Documentation about Route Parameters: Laravel Route Parameters
So this means you could just make your route like this.
Route::get('post/form/{id?}', array('as' => 'admin.post', 'uses' => "PostController#getForm"))
In your controller you then need to check if the 'id' is present. If not you create the user. If the 'id' is present you delete the user.
You can't use 1 route for 2 methods.
The solution is using 1 method which fires concrete method eg.
routes.php
get('post/form/{id?}', 'PostConteoller#form');
PostController.php
public function form($id = null) {
return $id ? $this->deleteForm($id) : $this->createForm();
}
However using 2 routes is much simpler.

Categories