I'm trying to write a if() condition with Blade, I have this :
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->username}}</td>
<td>{{$user->email}}</td>
<td>Supprimer le membre</td>
#if({{$user->admin}} == 0)
<td>Passer l'utilisateur Admin</td>
#else
<td>Retirer l'admin à l'utilisateur</td>
#endif
</tr>
#endforeach
I want to check if the user is admin or not (column in my users table) but the if returns me 3 errors :
first one : if(^here{{$user->admin}} == 0) -> Expected : condition
Seconde one : if({{$user->admin**^here**}} == 0) -> Expected : semicolon
Third one : if({{$user->admin}}^here == 0) -> Expected : Statement
I searched for a while how to fix it but I don't find, maybe someone could help me.
Thank you :)
Inside blade tags, you don't need to add another tag. You are currently trying to add {{ }} tag inside the #if() tag.
Try to think like this: {{ /* php code */ }} and #if(/* php code */).
So, to fix your problem, you simply write:
#if($user->admin == 0)
and that should be it.
Try this
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->username}}</td>
<td>{{$user->email}}</td>
<td>Supprimer le membre</td>
#if($user->admin == 0)
<td>Passer l'utilisateur Admin</td>
#else
<td>Retirer l'admin à l'utilisateur</td>
#endif
</tr>
#endforeach
{{}} is used to echo. You can directly access variable inside #if, like #if($user->admin == 0)
Related
This is data I have:
but with this blade template code:
<tbody>
#foreach ($orders as $order)
<tr>
<td>{{$order->id}}</td> //51 line
<td>{{$order->order->b_first_name}} {{$order->order->b_last_name}}</td>
<td>{{$order->order->r_first_name}} {{$order->order->r_last_name}}</td>
<td>£{{$order->total}}</td>
<td>{{$order->validity->diffForHumans()}}</td>
</tr>
#endforeach
</tbody>
I got this error:
ErrorException in fbe60fc17d23b35313269a941fc4d6f0 line 51: Trying to
get property of non-object (View:
/home/dgadmin/public_html/test/resources/views/admin/expired.blade.php)
What is the problem here? Why I cant use #foreach loop ?
First, go into your /storage/framework/views folder and look at line 51 for the file called fbe60fc17d23b35313269a941fc4d6f0. I see you said that it was $order->id, but I was not sure if you checked the compiled view that is actually served or admin/expired.blade.php. The compiled view is the one throwing the error, so the line number will correspond with the compiled view, not the blade you are making. When you look on this line you will see what is missing. This error is thrown because you are trying to access a null value on the model. If you want to go the lazy route, change your loop to this:
<tbody>
#foreach ($orders as $order)
<tr>
<td>{{isset($order->id)? $order->id: ''}}</td> //51 line
<td>{{isset($order->order->b_first_name)? $order->order->b_first_name : '' }} {{isset($order->order->b_last_name)? $order->order->b_last_name : ''}}</td>
<td>{{isset($order->order->r_first_name)? $order->order->r_first_name : ''}} {{isset($order->order->r_last_name)? $order->order->r_last_name : ''}}</td>
<td>£{{isset($order->total)?$order->total : ''}}</td>
<td>{{$order->validity->diffForHumans()}}</td>
</tr>
#endforeach
</tbody>
This will print the loop as expected, changing any missing values (like the one that triggered this errror) to a blank string.
Basically I want to #extend a blade template only if it exists, and if not #extend a different template. There are a few stack overflow answers concerning using #if #endif blocks, but that only works if you are #including a file, not #extending. Ideally something like this, but it doesn't work:
#if(some_condition == true)
#extends('one')
#else
#extends('two')
#endif
If the only way is to use Blade directives, could you please provide an example? Thank you!
Try doing it like this:
#extends( $somecondition == true ? 'one' : 'two')
you can use view:exists
#if(View::exists('path.to.view.one'))
#extends('one')
#else
#extends('two')
#endif
You can use View::exists
#if (View::exists('one'))
#extends('one')
#else
#extends('two')
#endif
or
file_exists() and to get the path use resource_path() this,
#if (file_exists(resource_path('views/one.blade.php')))
#extends('one')
#else
#extends('two')
#endif
You can try this, in my case that's working.. See the docs in Laravel - https://laravel.com/docs/5.5/views
#if( file_exists('path to file one'))
#extends('one')
#else
#extends('two')
#endif
you can use the conditional to define the name of the view you want to load and then simply extend it, for example:
#php
$view = '';
if (some_condition == true) {
$view = 'one';
} else {
$view = 'two';
}
#endphp
...
#extends($view)
more info
https://laravel.com/docs/5.5/blade#php
I have role column in users table, and I want to check the value like this in the blade file :
#if ( {{Auth::user()->role }} == '1')
// do something
#endif
Is it possible ?
In blade files, you need to write plain PHP into the #if and others blade statements. So you would need to remove the {{ }}:
#if ( auth()->user()->role == 1)
// do something
#endif
I think you can extend a blade.
https://laravel.com/docs/5.3/blade#extending-blade
It's cool and convenient.
Latest version of Laravel will work with like this. You don't need to use {{}} here.
#if ( Auth::user()->role == 1)
// do something
#endif
#if(\Illuminate\Support\Facades\Auth::user()->hasRole('Admin') == 'Admin')
// do something
#endif
So I have a messages table with all messages. I want pinned messages to be at top and normal ones to follow after. I have been able to do this normally with the orderBy() method in my query.
However, I want to display some specific messages for all my pinned messages. I would like to have a header at the top of the pinned messages and a header at the top of the normal messages to let users know that pinned and normal messages are there.
Example:
My query:
$rows = Messages::where('active', true)->orderBy('pinned', 'desc')->get();
My view
#foreach ($rows as $row)
{{ $row->message }}
#endforeach
What I see
Message text 3
Message text 1
Message text 2
I have a few messages with "pinned" in the column in database. So I want the pinned ones to show at the top WITH DESCRIPTIONS. Something like this:
Pinned
----------
Message text 3
----------
Normal
----------
Message text 1
Message text 2
I have tried orderBy() and it's working pretty good, in terms of ordering it from pinned to normal, but I can't get it to show the "Pinned" and "Normal" message. How can I do this?
Try something like this (change 1/0 to true/false or whatever you use):
In a controller:
$pinned = $rows->where('pinned', 1);
$normal = $rows->where('pinned', 0);
In a view:
#if(count($pinned) > 0)
Pinned
#foreach ($pinned as $row)
{{ $row->message }}
#endforeach
#endif
#if(count($normal) > 0)
Normal
#foreach ($normal as $row)
{{ $row->message }}
#endforeach
#endif
If real #foreach part is big, use partial and #each instead of #foreach to avoid code duplication.
Alternative
#foreach ($rows as $row)
#if ($row->pinned === 1 && !isset($pinnedShown))
Pinned
{{ $pinnedShown = true }}
#endif
#if ($row->pinned === 0 && !isset($normalShown))
Normal
{{ $normalShown = true }}
#endif
{{ $row->message }}
#endforeach
Short alternative
Not very readable, but if you just need short code, use something like this:
#foreach ($rows as $row)
<?php !($row->pinned == 1 && !isset($pin)) ? : $pin = call_user_func(function(){ echo 'Pinned'; return 1; });
!($row->pinned == 0 && !isset($nor)) ? : $nor = call_user_func(function(){ echo 'Normal'; return 1; }); ?>
{{ $row->message }}
#endforeach
I have the following foreach loop that I'm getting good results with however when it gets to the elseif statement I'm attempting to see if two of the players are on the same team or not and if they are then have them listed together separated with a & symbol. Currently I am getting the following still.
Player 1 vs. Player 2 vs. Player 3 vs. Player 4
This is okay however Player 1 and Player 2 are on the same team. So its not seeing them as on the same team for some reason. Does someone see what my issue is?
#foreach ($match->players AS $key => $player)
#foreach ($player->list as $member)
{{ $member->player_name }}
#endforeach
#if($match->players->count() - 1 != $key)
vs.
#elseif ($match->players[$key - 1]->team_id == $player->team_id)
&
#endif
#endforeach
EDIT: I changed the data a little but still should work.
http://pastebin.com/AdyzemC4
It was tricky to puzzle together your $match variable :)
#if($match->players->count() - 1 != $key)
Your $match->players->count() is always equal to the value of the number of players(say 'n'). So your 'if' ONLY checks the 'n-1' != current key ($key). [Which is true except for the last key, so you get all 'vs'].
This should work:
#foreach ($match->players AS $key => $player)
#foreach ($player->list as $member)
{{ $member->player_name }}
#endforeach
#if($player->team_id != $match->players[$key + 1]->team_id)
vs.
#elseif ($player->team_id == $match->players[$key + 1]->team_id)
&
#endif
#endforeach
In this we are checking if the current player's team is the same as the next player's team [$key+1].
Note:
You need to stop the loop for the last player, since $key+1 will go outside your array and you will get an offset error.
Therefore add another if:
#foreach ($match->players AS $key => $player)
#foreach ($player->list as $member)
{{ $member->player_name }}
#endforeach
#if($key + 1 < $match->players->count())
#if($player->team_id != $match->players[$key + 1]->team_id)
vs.
#elseif ($player->team_id == $match->players[$key + 1]->team_id)
&
#endif
#endif
#endforeach