Vpa
Vpa

Reputation: 737

relationship using with() in controller

I have two Model i.e., staff and attendance, and attendance table have staff_id

so in controller I want to display all staff with the relation of attendance like below

Staff::with('attendance')->get();

but the attendance should be formatted based on month selected

Is there any way to check the month value in my code like (where(attendance->month == request()->month)) or have to check the month in blade file (or another line)

thanks in advance and sorry for bad english

Upvotes: 0

Views: 50

Answers (2)

ali
ali

Reputation: 862

you can do the following:

$condition = '';
$data = Staff::whereHas('attendance',function($q) use($condition){
   $q->where('month',$condition);
})->get();

Upvotes: 1

void
void

Reputation: 913

try this:

$month = //month;
$data = Staff::with(['attendance', function($query) use ($month) {
                $query->where('month', $month);
              }])->get();

Let me know if it help you!

Upvotes: 2

Related Questions