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

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'])

Related

How to use if statement to compare value from a database in laravel?

I want to change the look of a label when the result from the database is equal a certain word. for instance. In my application when full payment is made, i want to change the appearance using bootstrap class. similarly, i want to change it when the user is owing.
This is what i did but it seem not to be working.
This is the error am getting syntax error, unexpected '<'
#if( {{$data -> status}} == "Full Payment" )
<label class="btn btn-primary">{{$data -> status}}</label>
#endif
#if( {{$data -> status}} == "owing" )
<label class="btn btn-danger">{{$data -> status}}</label>
#endif
how do i change the appearance depending on the results from the database
You don't need {{}} braces when you use blade tags (#), since PHP is already parsed inside them. This way when your code gets to first brackets ( {{ ) it renders them as <?php, and you get the syntax error. Change your conditions to this:
#if($data->status == "Full Payment" )
<label class="btn btn-primary">{{$data->status}}</label>
#endif
#if($data->status == "owing" )
<label class="btn btn-danger">{{$data->status}}</label>
#endif
What i would recommend doing if those are the only 2 possible outcomes is:
<label class="btn {{ $data->status == 'Full Payment' ? 'btn-primary' : 'btn-danger' }}">{{$data -> status}}</label>
You have to use like this
#if($data -> status == "Full Payment")
And
#if($data -> status == "owing")

How to include an HTML button inside Laravel code

I want to show a button if a condition is not true. In the code below I need to show the "follow group" button if the output is not true. It is working fine, but I don't know the syntax to put the button inside the Laravel code.
{{ Auth::user()->grpusers()->where('group_id', $group->id)->first() == True ? 'You are following this group' : Follow Group }}
You can simply use if else
<p>
#if( Auth::user()->grpusers()->where('group_id', $group->id)->first() == True)
You are following this group
#else
Follow Group
#endif
</p>

Correct #elseif syntax in Blade of Laravel

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']))

Laravel-5.3 : Updating session not working properly

I'm using database driver to store sessions. When I update the payload, the views, wich depend on the datas into the payload, don't update.
Into my view, I often have things like that:
#if( Session::get('quicktrade') == 'Client' )
<li class="#if ($coreData['navParent'] == 'quicktrade') active open #endif">
<a href="javascript:;">
<i class="ico-quicktrade-{{Session::get('left_menu')}}"></i>
<span class="title">QUICK[TRADE]</span>
<span class="arrow #if ($coreData['navParent'] == 'quicktrade') open #endif"></span>
#if ($coreData['navParent'] == 'quicktrade') <span class="selected"></span> #endif
</a>
<ul class="sub-menu">
#if( Session::get('quicktrade_evaluation') == 1 )
<li class="#if ($coreData['navChild'] == 'evaluation') active #endif">
<a href="{{ url('/quicktrade/evaluation') }}">
NOUVELLE ÉVALUATION</a>
</li>
#endif
#if ( Session::get('quicktrade_liste') == 1 )
<li class="#if ($coreData['navChild'] == 'liste') active #endif">
<a href="{{ url('/quicktrade/liste') }}">
LISTE DES VÉHICULES</a>
</li>
#endif
#if ( Session::get('quicktrade_statistiques') == 1 )
<li class="#if ($coreData['navChild'] == 'statistiques') active #endif">
<a href="{{ url('/quicktrade/statistiques') }}">
STATISTIQUES</a>
</li>
#endif
</ul>
</li>
#endif
This make the menu to show or not options depend on the session.
All the datas are added to the payload during the LogSuccesfulLogin event.
So, if the user logout then login, I got no problem when I do some changes in the users table.
The problem is when the session is active and the user don't logout. If I do some changes, the view will stay the same as before.
In my user controller there is what happen:
$user = User::with('acces')->find($user->id);
$sessions = DB::table('sessions')->where('user_id', $user->id)->get();
foreach ($sessions as $session) {
$payload = unserialize(base64_decode($session->payload));
$payload['oneaction'] = $user->acces->oneaction;
$payload['oneaction_type'] = $user->acces->oneaction_type;
if ($user->acces->oneaction == 'Client') {
$payload['oneaction_clients'] = $user->acces->oneaction_clients;
$payload['oneaction_list'] = $user->acces->oneaction_list;
$payload['oneaction_creator'] = $user->acces->oneaction_creator;
} else {
$payload['oneaction_clients'] = 0;
$payload['oneaction_list'] = 0;
$payload['oneaction_creator'] = 0;
}
//dd($payload);
$payload = base64_encode(serialize($payload));
DB::table('sessions')->where('id', $session->id)->update(['payload' => $payload]);
}
$sessions = DB::table('sessions')->where('user_id', $user->id)->get();
foreach ($sessions as $session) {
$payload = unserialize(base64_decode($session->payload));
//dd($payload);
}
As you can see in the code above, there is 2 dd(). Both show me that the changes are done properly. When I reload the page, ther is no change in the left menu.
I tried to do a php artisan view:clear right after the sessions have been updated, but the problem persist.
The only way I'm able to make this work, it's to delete all sessions of the user into the table. I don't want to go this way, so what am I missing? Why updating session don't work as I expect?
Your session gets probably lost because your response does not contain the session-cookie. In laravel this is done via the AddQueuedCookiesToResponse middlware which is kicked off in your HTTP-Kernel. But since you are dump and dying somewhere in your controller, you are preventing this middlware from running.
Please note that this is just a guess, there could be other reasons why your cookie is getting lost. Check your kernel and maybe also your config and ensure that you can see your cookie in your http response header.

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

Categories