Correct #elseif syntax in Blade of Laravel - php

Here is my code, I want to put an array of answers in just one #elseif line
#if( $selectSummaryTable == 'MemType' )
Summary of Members
#elseif( $report_type == ['Category', 'CivilStatus'] )
Baptism Report for {{ $report_date }}
#endif
How do you correctly put multiple values in the #elseif line?

You can use in_array function , change your code from
#elseif( $report_type == ['Category', 'CivilStatus'] )
to
#elseif(in_array($report_type,['Category', 'CivilStatus']))

Related

Issue with laravel IF statement when checking multiple "OR" conditions

In my laravel application, I'm trying to check multiple conditions inside a single if statement.
I'm trying to check the following conditions,
If the logged-in user's role_id is 1 and role_name is not equal to Admin or if the logged-in user's role_id is 1 role_name is not equal to Regional Admin, the button has to be disabled
#if((Auth::user()->role_id=='1' && $role->name!='Admin')||(Auth::user()->role_id=='1' && $role->name!='Regional Admin'))
<a class="btn btn-default btn_icon" href="{{ route('roles.edit',$role->id,false) }}"><img class="nc-icon" alt="edit" src="{{ asset('admin_icons/edit.svg') }}" data-toggle="tooltip" data-placement="top" title="Éditer" ></a>
But this condition keep fails. The button does not get disabled even if the both conditions are true...
You should follow the below if statement as per your requirement.
#if( (Auth::user()->role_id=='1') && ( $role->name != 'Admin' && $role->name!='Regional Admin' ) )
// Button disable
#else
// Show button
#endif
use something like that --
#if(Auth::user()->role_id=='1' && !in_array($role->name,['Admin', 'Regional Admin'])
As per your description you need to use AND condition instead of OR Condition
#if((Auth::user()->role_id=='1' && $role->name!='Admin')&& (Auth::user()->role_id=='1' && $role->name!='Regional Admin'))
<a class="btn btn-default btn_icon" href="{{ route('roles.edit',$role->id,false) }}"><img class="nc-icon" alt="edit" src="{{ asset('admin_icons/edit.svg') }}" data-toggle="tooltip" data-placement="top" title="Éditer" ></a>
Try this:
#if((Auth::user()->role_id=='1') && ($role->name!='Admin' || ($role->name!='Regional Admin'))
Make sure that:
=> role_id is field with type integer
=> $role->name is field with type varchar
and then replace your condition with below:
#if((Auth::user()->role_id == 1 && !($role->name === 'Admin'))||(Auth::user()->role_id == 1 && !($role->name === 'Regional Admin')))
used And method in laravel blade like this,
#if( (Auth::user()->role_id=='1') && ( $role->name != 'Admin' || $role->name!='Regional Admin' ) )
// show disable button
#else
#endif
Or also used the in_array method
#if(Auth::user()->role_id=='1' && !in_array($role->name,['Admin', 'Regional Admin'])

Check if a string contain any text

I have this string:
$mystring = "SIZE,DETAIL";
And I´m using:
#if (strpos($mystring, 'SIZE'))
{{ $item->size }}
#endif
#if (strpos($mystring, 'DETAIL'))
{{ $item->detail }}
#endif
But this works fine with SIZE, but not with DETAIL.
What is the problem here?
Since you're using Laravel, you can use str_contains() helper:
#if (str_contains($mystring, 'SIZE'))
The str_contains function determines if the given string contains the given value
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.
Try this:
#if (strpos($mystring, 'SIZE') !== false)
{{ $item->size }}
#endif
#if (strpos($mystring, 'DETAIL') !== false)
{{ $item->detail }}
#endif
refer: http://php.net/manual/en/function.strpos.php
When using strpos you need to compare to FALSE. An example of your blade code would be:
#if (strpos($mystring, 'SIZE') !== FALSE)
{{ $item->size }}
#endif
#if (strpos($mystring, 'DETAIL') !== FALSE)
{{ $item->detail }}
#endif
However, when using Laravel, you can use str_contains($haystack, $needles) instead of strpos.

Blade if() condition error

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)

How can I check auth user role in laravel?

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

Laravel 4.2 blade: check if empty

In Laravel blade you can do:
{{ $variable or 'default' }}
This will check if a variable is set or not. I get some data from the database, and those variables are always set, so I can not use this method.
I am searching for a shorthand 'blade' function for doing this:
{{ ($variable != '' ? $variable : '') }}
It is hard to use this piece or code for doing this beacuse of, I do not know how to do it with a link or something like this:
{{ $school->website }}
I tried:
{{ ($school->website != '' ? '{{ $school->website }}' : '') }}
But, it does not work. And, I would like to keep my code as short as possible ;)
Can someone explain it to me?
UPDATE
I do not use a foreach because of, I get a single object (one school) from the database. I passed it from my controller to my view with:
$school = School::find($id);
return View::make('school.show')->with('school', $school);
So, I do not want to make an #if($value != ''){} around each $variable (like $school->name).
try this:
#if ($value !== '')
{{ HTML::link($value,'some text') }}
#endif
I prefer the #unless directive for readability in this circumstance.
#unless ( empty($school->website) )
{{ $school->website }}
#endunless
With php 7, you can use null coalescing operator. This is a shorthand for #m0z4rt's answer.
{{ $variable ?? 'default' }}
{{ ($school->website != '' ? '{{ $school->website }}' : '') }}
change to
{{ ($school->website != '') ? '' . $school->website . '' : '' }}
or the same code
{{ ($school->website != '') ? "<a href='$school->website' target='_blank'>$school->website</a>" : '' }}
{{ isset($variable) ? $variable : 'default' }}
I wonder why nobody talked about $variable->isEmpty() it looks more better than other. Can be used like:
#if($var->isEmpty())
Do this
#else
Do that
#endif
From Laravel 5.4, you can also use the #isset directive.
#isset($variable)
{{-- your code --}}
#endisset
https://laravel.com/docs/9.x/blade#if-statements

Categories