I want to have a URL like this:
/v1/vacations?country=US&year=2017&month=08
How do I set the route in Laravel 5.3 and where can I put the controller and logic to accept the query string?
your route should look like;
Route::get('v1/vacations', 'VacationsController#index');
then on VacationsController
public function index()
{
dd(request()->query());
$query=request()->query();
//search database using query
//return view with results
}
Query strings can not be defined in your route since the query string is not part of the URI.
To access the query string you should use the request object. $request->query() will return an array of all query parameters. You can also use it as such to return a single query param $request->query('key')
You just would check the Request object for the url parameters like so:
// The route declaration
Route::get('/v1/vacations', 'YourController#method');
// The controller
class YourController extends BaseController {
public function method(Illuminate\Http\Request $request)
{
$country = $request->country;
// do things with them...
}
}
Hope this helps you.
Related
I have a route to get a single post item by slug.
Route
Route::get('post/{post}', 'PostController#details')->name('post.details');
While I want to pass the model in the controller method for the route.
Controller
public function details(Post $post)
{
// how to get the post by slug
}
My question is how can I get the post by slug passing in the route
instead of post ID?
I am aware that I can pass the slug and get the post using where clause.
//Route
Route::get('post/{slug}', 'PostController#details')->name('post.details');
//Controller method
public function details($slug)
{
$post = Post::with('slug', $slug)->first();
}
But I want to learn to do the same by passing the Model in the method.
set route key name to your model class
//Post.php
public function getRouteKeyName()
{
return 'slug';
}
This will inform Laravel injector/resolver to look the variable passed in slug column while fetching the object instance.
What you want to do is implicit route model binding
What you can do is in your Post model define getRouteKeyName like below
<?php
class Post extends Model
{
/**
* Get the route key for the model.
*
* #return string
*/
public function getRouteKeyName()
{
return 'slug';
}
}
and define your route like this:
Route::get('post/{post:slug}', 'PostController#details')->name('post.details');
and then in your controller
public function details(Post $post)
{
// it will return post with slug name
return $post;
}
Hope it helps.
Thanks
I've been working on a project for a few months now and I feel like I am seeing some inconsistency with how the public function show is working
I have a model and controller for a Location that has
public function show(Location $Location)
{
$Loc = Location::with('company:id,name')->findOrFail($Location);
return response()->json($Loc,200);
}
and that works just fine. Note the parameters.
I just made a new model and controller for Asset and it has this:
public function show(Asset $asset)
{
$AssetReturn = Asset::with('location:id,name,address')->findOrFail($asset);
return response()->json($AssetReturn,200);
}
but that does not work. it just returns empty. If i remove the class name from the parameters so its just
public function show($asset)
then it works as intended.
relation from asset model to location:
public function location()
{
return $this->belongsTo(Location::class);
}
According to the documentation, Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name. For example:
Route::get('/assets/{asset}', function (App\Asset $asset) {
$asset->load('location:id,name,address');
return response()->json($asset);
});
Since the $asset variable is type-hinted as the App\Asset Eloquent model and the variable name matches the {asset} URI segment, Laravel will automatically inject the model instance that has an ID matching the corresponding value from the request URI. If a matching model instance is not found in the database, a 404 HTTP response will automatically be generated.
If you don't want this behavior and want to use findOrFail manually:
Route::get('/assets/{asset}', function ($assetId) {
$asset = App\Asset::with('location:id,name,address')->findOrFail($assetId);
return response()->json($asset);
});
I am using Laravel 5.5.13.
My goal is to create an endpoint like this:
/api/items/{name}?kind={kind}
Where kind is optional parameter passed in by the query string.
My current routes in api.php looks like this:
Route::get('items', 'DisplaynameController#show');
My current controller is like this:
public function show(Request $request)
{
if ($request->input('kind') {
// TODO
} else {
return Item::where('name', '=', $request->input('name'))->firstOrFail();
}
}
I
I am currently using $request->input('name') but this means I need to provide ?name=blah in the query string. I am trying to make it part of the route.
May you please provide guidance.
The $name variable is a route param, not a query param, this means that you can pass it directly to the function as an argument.
So, if your route is like this:
Route::get('items/{name}', 'DisplaynameController#show');
Your function should be like this:
public function show(Request $request, $name) // <-- note function signature
{ // ^^^^^
if ($request->has('kind'))
{
// TODO
}
else
{
return Item::where('name', '=', $name)->firstOrFail(); // <-- using variable
} // ^^^^^
}
Another option is to get the variable as a Dynamic Property like this:
public function show(Request $request)
{
if ($request->has('kind'))
{
// TODO
}
else
{
return Item::where('name', '=', $request->name)->firstOrFail();
} // ^^^^^^^^^^^^^^
}
Notice that we access the name value as a dynamic property of the $request object like this:
$request->name
For more details, check the Routing > Route parameters and Request > Retrieving input sections of the documentation.
As stated in the documentation you should do:
public function show($name, Request $request)
Laravel will take care of the variable binding
In my application I have a page it called index.blade, with route /index. In its URL, it has some get parameter like ?order and ?type.
I want to pass these $_get parameter to my route controller action, query from DB and pass its result data to the index page. What should I do?
If you want to access the data sent from get or post request use
public function store(Request $request)
{
$order = $request->input('order');
$type = $request->input('type');
return view('whatever')->with('order', $order)->with('type', $type);
}
you can also use wildcards.
Exemple link
website.dev/user/potato
Route
Route::put('user/{name}', 'UserController#show');
Controller
public function update($name)
{
User::where('name', $name)->first();
return view('test')->with('user', $user);
}
Check the Laravel Docs Requests.
For those who need to pass part of a url as a parameter (tested in laravel 6.x, maybe it works on laravel 5.x):
Route
Route::get('foo/{bar}', 'FooController#getFoo')->where('bar', '(.*)');
Controller:
class FooController extends Controller
{
public function getFoo($url){
return $url;
}
}
Test 1:
localhost/api/foo/path1/path2/file.gif will send to controller and return:
path1/path2/file.gif
Test 2:
localhost/api/foo/path1/path2/path3/file.doc will send to controller and return:
path1/path2/path3/file.doc
and so on...
how pass and get params from one route to another
return redirect('/registration')->with('some_params', $input);
and here is registrationController index method:
public function index(){
$some_params = Request::get('some_params'); //no result
}
with method flashes data to the session, you have to retrieve the data using the Session::get method.
public function index(){
$some_params = Session::get('some_params');
}