I have two tables and relations as bellow user the user table:
| id | name | active |
|---|---|---|
| 1 | abc | 1 |
| 2 | xyz | Null |
| 3 | abx | 0 |
the book table:
| id | user_id | name | active |
|---|---|---|---|
| 1 | 1 | book1 | 0 |
| 2 | 2 | book2 | 0 |
| 3 | 1 | book3 | 0 |
relation is as this user->books (HasMany)
return $this->hasMany(Book::class,'user_id','id');
my query is as bellow
User::with('book')
->WhereHas('book', function($query) {
$query->where(['active'=> 1]);
})
->where(['id'=> 1,'active'=>1])
->get();
This query is getting zero records because active is 0 in books
- But i want to see all user record and if there is matching record with active 1 in book.
- second is query user for active 1 or Null and for that if use
->orwhereNull('active')All records changes.
Thanks