Home > other >  Nested foreach loop creating duplicate records with if condiotion - PHP Laravel 8
Nested foreach loop creating duplicate records with if condiotion - PHP Laravel 8

Time:12-19

Problem: There are modules, users, and user_modules tables, where the admin can assign multiple modules with permissions to a user. Admin can update module's permission which is already assigned to that user, and the modules which are not assigned should be loaded on blade view on the same table.

But the issue is data is duplicating

I am posting my code with images

AdminController:

$modules = Module::all();
$user_modules = User_module::with('module')->where('user_id', $user_id)->get();
return view('admin/seller_assign_modules', compact('user','modules','user_modules'));

seller_assign_modules.blade.php

<table >
    <thead>
        <tr>
           <th>Modules</th>
           <th>Add</th>
           <th>Edit</th>
           <th>View</th>
           <th>Delete</th>
        </tr>
    </thead>
    <tbody>
                            
     @foreach ($user_modules as $user_mod)
           @foreach ($modules as $mod)
                                
                @if ($mod->id == $user_mod->module_id)
                     <tr>
                           <td scope="row">{{$user_mod->module->name}}</td>
                           <td scope="row">{{$user_mod->add}}</td>
                           <td scope="row">{{$user_mod->edit}}</td>
                           <td scope="row">{{$user_mod->view}}</td>
                           <td scope="row">{{$user_mod->del}}</td>
                     </tr> 
                @else
                     <tr>
                           <td scope="row">{{$mod->name}}</td>
                           <td scope="row"></td>
                           <td scope="row"></td>
                           <td scope="row"></td>
                           <td scope="row"></td>
                     </tr>
                @endif

         @endforeach
     @endforeach

    </tbody>
</table>

modules table:

enter image description here

user_modules table:

enter image description here

result on seller_assign_modules.blade.php

enter image description here

I NEED THIS:

enter image description here

CodePudding user response:

You can do something like below (this is not a good approach however)

$user_modules = User_module::with('module')->where('user_id', $user_id)->get();
//Get modules where user dont have records
$non_user_modules = Module::whereNotIn('id', (clone $user_modules)->pluck('module_id')->toArray())->get();
        
return view('admin/seller_assign_modules', compact('user','modules','user_modules','non_user_modules'));

Then in the blade

 @foreach ($user_modules as $user_mod)
     
                 <tr>
                       <td scope="row">{{$user_mod->module->name}}</td>
                       <td scope="row">{{$user_mod->add}}</td>
                       <td scope="row">{{$user_mod->edit}}</td>
                       <td scope="row">{{$user_mod->view}}</td>
                       <td scope="row">{{$user_mod->del}}</td>
                 </tr> 
          

 @endforeach

 @foreach ($non_user_modules as $non_user_module)
     
                 <tr>
                       <td scope="row">{{$non_user_module->name}}</td>
                       <td scope="row"></td>
                       <td scope="row"></td>
                       <td scope="row"></td>
                       <td scope="row"></td>
                 </tr>
          

  @endforeach

Best approach you could do

Make a relationship at Module model like userModules()

Then get Modules like below

$modulesWithUser = Module::with(['userModules' => function($userQuery){
            $userQuery->where('user_id',$user_id);
        }])->get();

Then loop $modulesWithUser in blade and access its relationship inside loop to show users permissions.

CodePudding user response:

The problem with here is,Module has manyUser_modules, but you only need one.

So in this case, one approach is map a new property like user_module to each Module.

AdminController

$modules = Module::all();
$userModeules = User_module::where('user_id', $user->id)->get();

$module = $modules->map(function($module {
    $module->user_module = $userModules->firstWhere('module_id', $module->id);
});

return view(
    'admin/seller_assign_modules',
    compact('modules')
);

seller_assign_modules.blade.php

<tbody>
  @foreach($mdules as $module)
    <tr>
      <td scope="row">{{ $module->name }}</td>
      <td scope="row">{{ optional($module->user_module)->add }}</td>
      <td scope="row">{{ optional($module->user_module)->edit }}</td>
      <td scope="row">{{ optional($module->user_module)->view }}</td>
      <td scope="row">{{ optional($module->user_module)->del }}</td>
    </tr>
  @endforeach
</tbody>
  • Related