Laravel route including all $_GET params - php

I use Laravel 5.3 and I have named all my routes.
I want to use the route() function and include my $_GET params.
This is what I've tried :
<a href="{{ route('myRoute', ['id' => $id, 'slug' => str_slug($name)], request()->all()]) }}">
Or
<a href="{{ route('myRoute', [array_merge(['id' => $id, 'slug' => str_slug($name)], request()->all())]) }}">
For now, I got this error
ErrorException in UrlGenerator.php line 377: Array to string
conversion (View: ....
Is there a way to include all params ? I don't want to list them one by one. Thanks
EDIT
I had en error in my code, now it works with :
<a href="{{ route('myRoute', array_merge(['id' => $id, 'slug' => str_slug($name)], request()->all())) }}">

request()->all() and ['id' => $id, 'slug' => str_slug($name)] are arrays and you're trying to pass it as string. When you have a lot of data, it's better to pass it using POST method.

A cheap hack would be this:
<a href="{{ route('myRoute') . '?' . http_build_query(array_merge(['id' => $id, 'slug' => str_slug($name)], request()->all())) }}">
http_build_query turns an associative array into GET paramaters string (without the starting ?).

Related

Pass parameter to laravel voyager menu builder link() method

I made a navbar using voyager's menu builder. I'm trying to add localization functionality to my app but can't figure out how to pass a {lang} parameter to this line of code:
#foreach ($items as $menu_item)
<a href='{{ $menu_item->link() }}' class="nav-link">
#endforeach
Normally I would do this:
Shop
I tried doing this which didn't work:
#foreach ($items as $menu_item)
<a href='{{ route($menu_item->link(), App::getLocale()) }}' class="nav-link">
#endforeach
Any ideas?
route() method is used only if you have named routes. If link() only returns a URL, you should not use it with the route() method.
I see one that you want added by current language, like http://yoursite/en. Here are two methods you can follow to solve your problem:
Create middleware and redirect according to current language. Use middleware by grouping your routes.
Revise $menu_item->link() to give you the route name and use the route() method.
SOLVED: running dd($menu_item) gave me these attributes:
#attributes: array:13 [▼
"id" => 18
"menu_id" => 2
"title" => "Shop"
"url" => ""
"target" => "_self"
"icon_class" => null
"color" => "#000000"
"parent_id" => null
"order" => 1
"created_at" => "2023-02-12 07:31:09"
"updated_at" => "2023-02-13 07:45:46"
"route" => "shopIndex"
"parameters" => null
]
$menu_item->route gives the route name which I can put in a route helper and pass the parameters I want.
Note: some links don't have a route name so you need to define a conditional in case of the links without a route name.
<a href='{{ route($menu_item->route ? $menu_item->route : 'cartIndex', App::getLocale()) }}' class="nav-link">

laravel 5 route passing two parameters

i have this route
Route::get('/artist/{id}/{name}', 'HomeController#artist')->where(['id' => '[0-9]+', 'name' => '[a-z]+'])->name('artist');
and this is my link
{{$artist->name}}
and this is the artist method on HomeController
public function artist($id, $name){ $artist = Artist::where('id', $id)->where('name', $name)->first();
return view('front.artist', compact('artist'));
}
i donot know this display error. this is the error. so please any one help me with this. iam in the middle of learning laravel.
ErrorException in UrlGenerationException.php line 17:
Missing required parameters for [Route: artist] [URI: artist/{id}/{name}]. (View: C:\xampp\htdocs\laravel\resources\views\front\home.blade.php)
Yo must pass the parameters as array, see https://laravel.com/docs/5.4/helpers#method-route
route('artist',['id' => $artist->id, 'name' => $artist->name])
or you can use
{!! link_to_route('artist', $artist->name, ['id' => $artist->id, 'name' => $artist->name]) !!}

Get specified properties of a model all at once in Laravel

I want to pass some user data to a view so it can be displayed in the profile page. There is quite a lot of it but I don't want to just pass everything, because there are some things the view shouldn't have access to. So my code looks like this:
return view('profile', [
'username' => Auth::user()->username,
'email' => Auth::user()->email,
'firstname' => Auth::user()->firstname,
'country' => Auth::user()->country,
'city' => Auth::user()->city->name,
'sex' => Auth::user()->sex,
'orientation' => Auth::user()->orientation,
'age' => Auth::user()->age,
'children' => Auth::user()->children,
'drinking' => Auth::user()->drinking,
'smoking' => Auth::user()->smoking,
'living' => Auth::user()->living,
'about' => Auth::user()->about,
]);
My question is: Can this be written shorter/simpler?
Thanks!
EDIT:
I don't want this: {{ Auth::user()->firstname }} because there is a logic in a view, which is bad - I think, there should be just plain variables to be displayed, in view, not anything else.
So I'm looking for something like:
return view('profile', Auth::user()->only(['firstname', 'email', ...]));
You could create by yourself a method named like getPublicData and then return all those properties you need.
...
public function getPublicData() {
return [
'property_name' => $this->property_name
];
}
...
... and then use it in your controller/views. Maybe it's not an optimal solution, but you can isolate this thing in the model and avoid too much code in the controller.
Another advanced approach could be the override of the __get method. However, I am not the first in this case.
Hope it helps!
You don't need to pass these variable to the view, you can directly access them in the view using blade
<h1>{{ Auth::user()->username }}</h1>

How to create a volt URL with parameters

I want to create a URL using volt (Phalcon).
I have tried:
{{ url("order/view/", ["id" :order.id]) }}
However that produces a URL like:
http://localhost/gateway-new/order/view/?id=7
Whereas I would like the url to look like:
http://localhost/gateway-new/order/view/id/7
Any idea how to do this correctly?
if you have a route defined like
$router->add('order/view/id/:int', array(
'controller' => 'order',
'action' => 'view',
'id' => 1))->setName('order-view');
you could use
{{ url(['for': 'order-view', 'id': order.id]) }}
{{ url("order/view/id/" ~ order.id) }}

Laravel nested resource route parameters in html forms & urls

Im essentially trying to see if there is a more efficient or proper way of accessing route parameters in the views of a nested resource. The code below demonstrates what I'm doing, catching all parameters from the route: /schools/1/classes/2/teachers/4/assignments
into the controller index method, and then making a view and passing it all of those parameters so that within the view I can make forms and links that use the same route format & parameters. Is there a better way? Laravel Paste
//
// app/routes.php
//------------------------------------------------------
Route::resource('schools.classes.teachers.assignments', 'AssignmentsController');
//
// app/controllers/AssignmentsController.php
//-------------------------------------------------------
public function index($school_id,$class_id,$teacher_id)
{
$routes = array($school_id,$class_id,$teacher_id);
$assignments = $this->assignment->all();
return View::make('assignments.index', compact('assignments'))
->with('routes', $routes);
}
//
// app/views/assignments/index.blade.php
// ------------------------------------------------------------
<p>{{ link_to_route('schools.classes.teachers.assignments.index', 'All Assignments', array($routes[0],$routes[1],$routes[2])) }}</p>
//
// app/views/assignments/edit.blade.php
// -------------------------------------------------------------
{{ Form::model($assignment, array('method' => 'PATCH', 'route' => 'schools.classes.teachers.assignments.update', $routes[0],$routes[1],$routes[2],$route[3]))) }}
-
You always need to pass the parameters and this is simple but I think it would be better if you use an associative array like this instead:
$routes = compact('school_id', 'class_id', 'teacher_id');
So it'll become:
$routes = array(
'school_id' => $school_id,
'class_id' => $class_id,
'teacher_id' => $teacher_id
);
So, you can use:
{{ Form::model($assignment, array('method' => 'PATCH', 'route' => 'schools.classes.teachers.assignments.update', $routes['school_id'], ['class_id'], ['teacher_id']))) }}
Looks more readable and easy to understand.

Categories