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">
Related
Is it possible to use a variable as the call to a Laravel Blade directive?
For my menu system, I've defined a component and I would like to be able to set the visibility of a link in the component. I have created numerous Blade directives (staff, admin, client, etc.) which check a user's role.
My component definition looks like this:
#component('components.primaryMenu', [
'items' => [
[
'route' => route('some.uri'),
'visibility' => ['staff', 'client'],
'label' => 'Item 1',
],
[
'route' => route('another.uri'),
'visibility' => ['everyone'],
'label' => 'Item 2',
],
]
])#endcomponent
What I would like to do is:
<ul class="nav">
#foreach($items as $item)
#foreach($item['visibility'] as $visibility)
#{{ $visibility }} // Should interpolate to #staff / #client
// Link stuff in here
#end{{ $visibility }} // Should interpolate to #endstaff / #endclient
#endforeach
#endforeach
</ul>
When I run this code I get "Invalid argument supplied for foreach()". I'm guessing because the #{{ $visibility }} declarations are throwing off the parser.
My Blade directives are defined in a service provider and look like this:
Blade::if('staff', function () use ($user) {
return $user->isType('staff');
});
Blade::if('client', function () use ($user) {
return $user->isType('client');
});
i am building a laravel aplication and i have this line of code which should redirect the user back to form he just submited , with the old input and the result of some operations .
return back()->with(["result" => round($area, 2)])->withInput($request->all());
The problem is that i only receive the old input in blade and the $result variable is not available in the view.
This is how i try to output the result:
<input type="text" name="result" value="{{isset($result)&&old('roofType')==0?$result:''}} ㎡ " class="form-control input-sm" >
And here is what variables i have in the view after submit:
{{ dd(get_defined_vars()['__data']) }}:
array:7 [▼
"__env" => Factory {#89 ▶}
"app" => Application {#3 ▶}
"errors" => ViewErrorBag {#169 ▶}
"roofName" => "Acoperis intr-o apa"
"roofType" => "1"
"roofFolder" => "A1"
"baseFields" => array:3 [▼
0 => "L"
1 => "l"
2 => "H"
]
]
The problem was that I thought that writing
return back()->with('bladeVar', $controllerVar) was the same as return view('test')->with('bladeVar', $controllerVar);,but it wasn't .
You cannot echo a variable using blade normal syntax: {{ $bladeVar }}, Instead, you have to access the session to get the value: {{ session('bladeVar') }}.
After I changed the way I displayed the data all worked as expected.
The answer is you can not.
If you want to use with() then use it with view() like:
return view('welcome')->with(['name' => 'test']);
You can not use with() with back() and redirect(). It won't give you any error but you will not get the variable on the view.
More info: https://laravel.com/docs/master/views#passing-data-to-views
return redirect()->back()->with('result',round($area, 2))->withInput($request->all());
call
{{Session::get('result')}}
in your blade.
return view('profile.reset', compact('user'));
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 ?).
I am making an online shop, so it has products. I am outputing all the products images and their names and I want when the user clicks on the product to redirect him to a single-product page the problem I have is passing the id of the product to the single-product view.
Here's my code Routing:
Route::get('single', [
"uses" => 'ProductsController#single',
"as" => 'single'
]);
Index.blade.php:
See product
And the controller:
public function single($product_id)
{
$product = Product::where('product_id', $product_id);
return view('single-product', compact("product"));
}
You need to capture segments of the URI within your route.
Route::get('single/{id}', [
"uses" => 'ProductsController#single',
"as" => 'single'
]);
Make changes to your route as given below:
Route::get('single/{product_id}', [
"uses" => 'ProductsController#single',
"as" => 'single'
]);
If you want to pass any parameters to your route then you've to assign placeholders for the parameters, so in your case, the {product_id} will be used as the placeholder which will be used to take the parameter from the URI, for example: http://example.com/single/1. So, you'll receive the 1 as $product_id in your single method.
I have an $alerts variable array.
Look like this
array:3 [▼
0 => array:3 [▼
"status" => 200
"message" => "Success"
"data" => []
]
1 => array:3 [▼
"status" => 200
"message" => "Success"
"data" => []
]
2 => array:3 [▼
"status" => 404
"error_code" => 35
"message" => "invalid json - api not supported"
]
]
I want to send it from my controller to my view.
I've tried this
controller
return Redirect::to('/account/'.$id)
->with('alerts',$alerts)
Route
My route : http://localhost:8888/account/1007
View
I tried accessing like this
{!!$alerts!!}
Then, I tried accessing it like this, but I kept getting
Undefined variable: alerts
As per the Laravel documentation on redirects, when redirecting using with() it adds the data to the session, and not as a view variable. Therefore, you will need to access it like:
#foreach (session('alerts') as $alert)
<p>{{ $alert['message'] }}</p>
#endforeach
Try this:
Session::flash('alerts', $alerts);
return route('ROUTENAME', $id);
Just change ROUTENAME, in the name of the route (if defined in routes.php).
For example:
Route::get('account/{id}', ['as' => 'account.show', 'uses' => 'AccountController#show']);
In this example, ROUTENAME would be 'account.show'.
In your view you can access it by doing:
Session::get('alerts');
Info:
- http://laravel.com/docs/5.1/session#flash-data
Sometimes you may wish to store items in the session only for the next request. You may do so using the flash method.
You haven't posted the code that actually loads the view. When you return a Redirect->with(...) all you're doing is passing the variable to the next request. In your controller that serves the account/{id} route you need to return view('viewname', ['alerts' => session('alerts')])