J
J
Jedi2020-05-12 23:59:41
Laravel
Jedi, 2020-05-12 23:59:41

How to filter records using Eloquent?

Tables

Cars

- id
- name
- user_id

Models
- id
- car_id
- title

How to get cars (Car) whose name is more than 10 characters and only if it has at least one (Model) from the User model.

The question is not about hasMany, BelongsTo, but how to arrange this check? How many documentation I do not read, I can not figure it out, please help.

Now the code looks like this..

/**
     * Get the all Cars with Models by me.
     */
    public function cars()
    {
        return $this->hasMany('App\Cars')->with('models');
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2020-05-13
@PHPjedi

$user->cars()
     ->whereNotNull('title')
     ->has('models')
     ->get();

It's just not clear how cars can not have models and how title can be null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question