Questions tagged [wherehas]
32 questions
2
votes
1 answer
In eloquent : How to get a model that has the count of a related model exactly n with a condition?
In eloquent : How to get related a model where count of related models with a condition is is exactly n?
Here is over simplification of the problem that I am facing:-
There are multiple courses in database.
One course has many students.
I need to…

Jasmeet Singh
- 499
- 5
- 6
2
votes
2 answers
Multiple orWhere() in whereHas() in Laravel 5
Contact Model
class Contact extends Model
{
public function Account()
{
return $this->belongsTo('app\Account');
}
}
Account Model
class Account extends Model
{
public function Contact()
{
return…

paranoid
- 6,799
- 19
- 49
- 86
1
vote
0 answers
Eloquent query : Retrieve the list of offices where the user possess all the desks, not just one (nested whereHas)
I want to retrieve all the offices ( with the desks eager loaded) but I only want offices where the user possess all the desks in the office
I have the following models and relationships between them :
I came up with the following query which seems…

dib258
- 705
- 8
- 25
1
vote
1 answer
Laravel Eloquent multiple whereHas on relationship where column 'order' is higher than the previous whereHas
I'm working on a bus route system with stops at certain locations.
The route stops are in a ascending sequence by a 'order' column, as in 1, 2, 3, 4, 5.. etc.
These are my tables:
BUS
id | operator_id | name
1 | 1 | The Big Red…

Adriaan
- 376
- 1
- 6
- 21
1
vote
1 answer
Laravel Eloquent Retrieve Records Which Has Null Fields Included Relationship
I am trying to get records which has empty fields with relationship. And my Deal model like that.

Yoruichi
- 11
- 3
1
vote
3 answers
How To Use groupBy in whereHas laravel
i am using Metable in my project for creating meta for orders
but i have one problem
i want group orders that have same email
this is metable table image : image 1 image 2
i need code like this can work :)
Order::whereHas( 'meta', function($query)…

Dr Web
- 13
- 4
1
vote
1 answer
Relationship filter in whereHas callback with Eloquent
Given the following models
Conversation: id, user_id,
Message: id, conversation_id, author_id, created_at
I wanted to query all conversations who have messages between $start and $end, so i did this:
$filterMessages = function($query) use ($start,…

pelomedusa
- 47
- 1
- 11
0
votes
1 answer
How to use groupby in whereHas
I have two tables variants and variant_attributes which have one to many relation.
Each variant has one or more rows in variant_attributes table.
A group of variants could have these
[
1 => [13, 1, 18],
2 => [13, 2, 18],
3 => [13, 3, 18],
4…

nashwa ghazy
- 29
- 9
0
votes
1 answer
Laravel: pass an argument to the relationship function (not the query callback function) when using whereHas
I would like to know how to pass an argument to a model relationship function. Just to be clear, I'm NOT talking about the query callback.
Consider a model like so:
class UserRelationships extends Model
{
// other stuff
// dynamic scope:
…

Valentino
- 465
- 6
- 17
0
votes
3 answers
Can I use for loop in laravel 9 WhereHas ? ( filter data )
I'm trying to build a filter on my data using laravel ..
I have many to many relationships between the BasicItem model And AttValue Model and table Item_value between them.
this code works as what I want but I need it to be more dynamic depending on…

Ammar
- 3
- 2
0
votes
1 answer
Laravel calculate the value inside wherehas and return data
$credits = User::select('id','username')->where('id', auth()->id())
->with(['game_credits' => function($query) use ($phase){
$query->select('user_id','no_of_game_played','game_credits')
->where('phase_id',…

Nis shres
- 39
- 5
0
votes
1 answer
how to join 3 table wherehas eloquent using laravel
for example I have 3 tables and that tables has a relation,
Table A
id
name
Table B
id
tableId_A
tableId_C
Table C
id
name
i'm using table C but i want to search items by request name that have the same name as in table A using query…

Marc Jamal
- 47
- 1
- 2
- 12
0
votes
1 answer
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel whereHas returning error
Hello I want to filter my response by tags and categories in laravel 9. my code is :
$posts = Post::when($categoryId > 0, function($query) use ($categoryId) {
return $query->where('category_id', '=', $categoryId);
})
…

Saeed Baboui
- 1
- 1
0
votes
1 answer
Filtering unanswered tickets with the query builder
I need to generate a table of tickets not answered by the user, in this case the student. I would like to make the filter directly in the DB because it depends on other filters and there is also…

Klethonio
- 41
- 4
0
votes
0 answers
Livewire filter with multiple conditions : How to?
I am in trouble with a little functionnality I want to add in my project, sure someone can help me :)
The stack is Laravel with Livewire to add easily some interactivity.
I have a table with a list of bills. I use Livewire pagination in this table,…

Yann Pollet
- 15
- 8