Home > Software design >  Issue with laravel IF statement when checking multiple "OR" conditions
Issue with laravel IF statement when checking multiple "OR" conditions

Time:09-30

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...

CodePudding user response:

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

CodePudding user response:

use something like that --

   @if(Auth::user()->role_id=='1' && !in_array($role->name,['Admin', 'Regional Admin'])

CodePudding user response:

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>

CodePudding user response:

Try this:

@if((Auth::user()->role_id=='1') && ($role->name!='Admin' || ($role->name!='Regional Admin'))

CodePudding user response:

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

CodePudding user response:

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