dimanche 28 février 2021

Is there a way to fetch all users details who do not have certain value in the child table in laravel 5

I have two tables users, whose model is User and an immoveables table with model Immoveables, and have one to many relationship respectively.

I have immoveables table highleighted as: immovebales table The column year means that the user has submitted immoveables details for a particular year.

I have the code in my controller as:

$users=User::with('immoveables')->where('department_id',$request->department)
    ->where('role_id','2')->where('full_name','!=','commissioners')->orderBy('full_name')->get();
    $departments=Department::all();
    $years=Immoveable::groupBy('year')->latest()->pluck('year');
    return view('defaulter.test',compact('departments','users','years','department'));

and in my view as:

@foreach ($users as $user)
     @foreach ($user->immoveables as $value)
     <tr class="content">
       <td style="width: 16.66%">
          <br> <div><b></b></div>
       </td>
       <td style="width: 16.66%"></td>
       <td ></td>
       <td style="width: 16.66%"></td>
       <td style="width: 16.66%"></td>
     </tr>
     @endforeach       
@endforeach

The user can submit details of his immoveables yearly, so one user can have immoveables for 2018,2019,2020... What I want to achieve is to get all users who have not submitted immoveables for the year 2020 but submitted for the year 2019.

But the above codes will display all data from both users and immoveables tables. Is there a way to get all users who have not submitted immoveables for 2020, that is users who do not have data with value 2020 in the year column in the immoveables table, but has 2019.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire