0

I am working with laravel. I have two table. one is users and another is permission. I have two foreach loop. One is to show user data. User data coming from users table. And another foreach loop is is for checking is that user available or not in permission table. I am fetching all user from users table like this:

    $allTeachers = User::where('role',2)->paginate(4);

And am also fetching all data from permission table like this.

$allpermiteds = Specialpermission::where('add_teacher','!=','NULL')->get();

I have sent those two query to my view. And it is working fine. Now I am printing those users data in my view like this

@foreach($allteachers as $teacher)
            <tr> 
              <td>{{ $teacher->id}}</td>
              <td>{{ $teacher->name }}</td>
              <td>{{ $teacher->email }}</td>
              <td><button>give</button></td>
              <td><button>Remove</button></td>
@endforeach

I have printed two button there give and remove. Now I want to do that, If $teacher->id is available in the permission table add_teacher column then it should show remove button. Otherwise it should Show add button. I have done like this, But it is not working properly.

@foreach($allteachers as $teacher)
        <tr> 
          <td>{{ $teacher->id}}</td>
          <td>{{ $teacher->name }}</td>
          <td>{{ $teacher->email }}</td>
          <td>@foreach($allpermitted as $p)
        @if($p->add_teacher == $teacher->id)
        <button>Remove</button>
        @else
       <button>give</button>
        @endif
@endforeach</td>
@endforeach

It shows a lot of button. If there are 4 users in the permission table then it will show 4 button for each user. That is the main problem

Thanks in advance.

2 Answers 2

1

Simple, select add_teacher column from db, then convert your Specialpermission objects into array like below:

$permissionData = Specialpermission::where('add_teacher','!=','NULL')->select('add_teacher')->get();
$allpermiteds   = collect($permissionData )->map(function($x){ return (array) $x; })->toArray();

Then use in_array to check for permission check

@foreach($allteachers as $teacher)

    <tr> 
      <td>{{ $teacher->id}}</td>
      <td>{{ $teacher->name }}</td>
      <td>{{ $teacher->email }}</td>
      <td>
        @if(in_array($teacher->id,$allpermitted))
        <button>Remove</button>
        @else
       <button>give</button>
        @endif
    </td>

@endforeach
Sign up to request clarification or add additional context in comments.

4 Comments

thanks, but it is only print give . I mean if condition is not working. If I do {{dd($allpermitted)}} before if condition in view I can see those data. But if condition is not working . It print else condition .
@Fokrule: Try this: @if(in_array($teacher->id, $allpermiteds))
@HirenGohel both are same. If I send $allpermiteds then i should $allpermiteds and also if I send $allpermited to my view from controller then I should use $allpermited .
$allpermitedsTmp = collect($permissionData )->map(function($x){ return (array) $x; })->toArray(); then $allpermiteds= []; foreach($allpermitedsTmp as $item){ $allpermiteds[] = $item->add_teacher; } Now your $allpermiteds array contains only teachers id, now if (in_array()) logic will works.
0

I prefer using the eloquent function with that defines the relationship between the two tables:

$allTeachers = User::where('role',2)->with('SpecialPermisson')->paginate(4);

It will add a field of SpecialPermission (if it has). If not, it will give you a blank field, so check if its empty.

3 Comments

can you please explain some more kindly. I mean what will be eloquent relationship between those two table, and then how I should check if condition. please @sushant
@Fokrule in models that you define user and Special permisson you add a function that defines the relationship among them link see this link for more data It will always return data as usual with an extra field of specialPermisson you will see it if u dd the $allteachers and check if the field is empty using the php empty function
yap if there are no special permission then it will show null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.