Reputation: 821
I am trying to do the following query model in Laravel:
$num_stamps = Groupsheetstamp::where('status', '=', 'active')
->where('user_id', '=', $user->id)
->whereGroup_sheet_id($sheet->id)
->orWhere(function ($query) use ($sheet, $user) {
$query->where('original_group_sheet_id', $sheet->id);
})
->get();
However, this still returns all groupstamps that have a status
of inactive
, and it completely looks over the where clause for user_id
.
What am I doing wrong in the query model, since it isn't querying the correct result back?
An example output from the query could be the following, with user id = 7
and sheet id = 12
"id" => 23
"group_sheet_id" => 12
"groupsheet_id" => 12
"group_id" => 3
"original_group_sheet_id" => 12
"user_id" => 1
"comment" => ""
"status" => "inactive"
"start_time" => "2017-03-16 17:09:06"
"end_time" => "2017-03-16 23:06:39"
"stamp_duration" => 6
"hours" => 0
"minutes" => 0
"themonth" => "03.17"
"manual_time" => ""
"created_at" => "2017-03-16 23:06:33"
"updated_at" => "2017-03-16 22:09:06"
Upvotes: 1
Views: 41
Reputation: 7371
try this
$num_stamps = Groupsheetstamp::where('status', '=', 'active')
->where('user_id', '=', $user->id)
->where('group_sheet_id',$sheet->id)
->orWhere(function ($query) use ($sheet, $user) {
$query->where('original_group_sheet_id', $sheet->id);
})
->get();
Upvotes: 0
Reputation: 163748
It depends on what exactly result you're expecting. If you want to to search by group or by relation, you should use the closure. Just an example:
$num_stamps = Groupsheetstamp::where('status', 'active')
->where('user_id', $user->id)
->where(function($q) use ($sheet, $user) {
$q->where('group_sheet_id', $sheet->id)
->orWhere('original_group_sheet_id', $sheet->id);
})
->get();
https://laravel.com/docs/5.4/queries#parameter-grouping
Upvotes: 1