I have a few blade template files which I want to include in my view dynamically based on the permissions of current user stored in session. Below is the code I've written:
#foreach (Config::get('constants.tiles') as $tile)
#if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
#include('dashboard.tiles.' . $tile)
#endif
#endforeach
Blade is not allowing me to concatenate the constant string with the value of variable $tile. But I want to achieve this functionality. Any help on this would be highly appreciated.
You can not concatenate string inside blade template command. So you can do assigning the included file name into a php variable and then pass it to blade template command.
#foreach (Config::get('constants.tiles') as $tile)
#if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
<?php $file_name = 'dashboard.tiles.' . $tile; ?>
#include($file_name)
#endif
#endforeach
Laravel 5.4 - the dynamic includes with string concatenation works in blade templates
#foreach (Config::get('constants.tiles') as $tile)
#if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
#include('dashboard.tiles.' . $tile)
#endif
#endforeach
Related
Good, in php there is a trim function to remove the white spaces, is there a function for blade?
In php it would be like this:
trim($title)
I think I understood your question, what I want is to go from this test text to testtext
You can use #php ... #endphp to call any PHP functions inside blade file. Just to use trim function example is given below.
// #php is similar to <?php ?>
#php
$mytitle = trim($title)
#endphp
// Echo my title
{{ $mytitle }}
#foreach($last_articles->chunk(3) as $row)
#foreach($row as $last_article)
#if($loop->index!=0) {{"in $loop"}} #endif
#endforeach
#if($loop->index!=0) {{"out $loop"}} #endif
#endforeach
how to access the $loop of any of foreach, that was used in the code?
You could use $loop->parent to access the parent loop of the current loop.
In my case (Laravel 6.8) $loop->parent looks like this
[
{"iteration":1,"index":0,"remaining":1,"count":2,"first":true,"last":false,"odd":true,"even":false,"depth":1,"parent":null},
{"iteration":2,"index":1,"remaining":0,"count":2,"first":false,"last":true,"odd":false,"even":true,"depth":1,"parent":null}
]
to access the parent index $loop->parent->index
In my config/locale.php I have an array 'displayLanguage' which contain key=>value pairs
How can I loop through this array in blade ?
I have tried the following
#foreach ( {{ Config::get('app.locale.displayLanguage') }} as $itemKey => $itemVal)
{{ $itemKey }}
#endforeach
I am getting syntax error, unexpected '<'. tried also some other veriation to loop this var without passing it through the controller
If your file is in config/locale.php then you call config('locale.displayLanguage');
#foreach(config('locale.displayLanguage') as $key => $value)
{{ $key }}
#endforeach
I am using the global helper config() in a blade file.
It also appears you have extra curly braces in your foreach loop
I was trying to use the #forelse in laravel but it give me this error
Undefined variable: data
is #forelse remove from laravel 4.2.4? because that's the version that i am using. This is my code
in my view
#forelse ($result as $data)
<tr><td> $data->name </td></tr>
#empty
<tr><td>No name match</td></tr>
#endforelse
in my controller
$result = User::find(1)->get();
return View::make('view')->with('result', $result);
I don't think #forelse has been around since Laravel 3. I could be wrong. I know it was removed at some point though. You need to use a standard #if and #foreach now.
#if (empty($result))
<tr><td>No name match</td></tr>
#else
#foreach ($result as $data)
<tr><td> $data->name </td></tr>
#endforeach
#endif
UPDATE
As pointed out by Antonio it was brought back and is available in v4.2.7+, so you'll need to update if you want it.
I found the same thing. In the laracast video on Laravel 5 it is mentioned. Although right after he explains that a better practice is using the if else with the count function:
#if (count($result))
has result
#ifelse
has no result
#endif
I am trying to make a drop down list in Laravel with the select options being values from my database and I have some issues. In other tutorials in this site, doing drop down list inquires building the models for your database. I have not created model classes and I do not intend to.
Routes.php
Route::get("/user/charname", array(
'as' => 'profile-character',
'uses' => 'ProfileController#dropDownList'
));
ProfileController.php
class ProfileController extends BaseController
{
public function dropDownList()
{
$list = DB::table('characters')->where('char_id', '128')->pluck('char_name');
return View::make('layout.profile')->with('character_options',$list);
}
}
In the profile.blade.php (view)
<div class="selected_char">
<form action="{{ URL::route('profile-character') }}" method="post">
<li>
{{ Form::select('character_options', $list ,Input::old('character_options')) }}
</li>
</form>
</div>
By doing this it says that $list from my view is undefined. Where do I define $list in the view and how will I carry $list from my Controller to the View because this line doesn't seem to do it's job
return View::make('layout.profile')->with('character_options',$list);
You need to use $character_options not $list in the View.
You actually specify that the variable should be 'character_options' in your View::make() call, so you need to refer to it as $character_options.
Additionally, ->lists('char_name') is better than using ->pluck('char_name') as it'll give you the full list. Pluck just returns the first item it finds.
Additionally to that, using ->lists('char_name', 'id') gets you the list keyed by the id column, which would be useful if you were to use this list to determine IDs for a foreign key field. But no biggie if not.
I have partially solved the issues by doing this in my view:
{{ Form::select('list', DB::table('characters')->where('char_id', '128')->lists('char_name') ,Input::old('list')) }}
Personally I do not see it as a viable solution, but a temporary one
{{ Form::select('character_options', $character_options ,Input::old('character_options')) }}
Error: undefined variable character_options
{{ Form::select('character_options', $list ,Input::old('character_options')) }}
Error: undefined variable list
Maybe I do not understand how i send the variable from the Controller to the View.
You need to use lists as mentioned by #alexrussel. Do note that View::make('layout.profile')->with('character_options',$list);, here you are passing the variable $character_options with the value contained in the $list and $list will no longer be available to the view.
ProfileController.php
<?php
class ProfileController extends BaseController
{
public function dropDownList()
{
$list = DB::table('characters')->where('char_id', '128')->lists('char_name');
return View::make('layout.profile')->with('character_options',$list);
}
}
profile.blade.php
<div class="selected_char">
<form action="{{ URL::route('profile-character') }}" method="post">
<li>
{{ Form::select('character_options', $character_options ,Input::old('character_options')) }}
</li>
</form>