W
W
WebDev2016-04-13 18:35:46
Laravel
WebDev, 2016-04-13 18:35:46

Condition for with in laravel?

Tell me how to set the condition correctly.
There are two entities

#
class Journal {
    public function posts()
    {
        return $this->hasMany('App\Models\Post');
    }
}
class Post {
    public function journal()
    {
        return $this->belongsTo('App\Models\Journal');
    }
}

Is there any way I can filter posts by journal in a query like this:
$posts = Post::with('journal')->get();
I need to add a condition for a journal so that only posts from those journals are selected.
It is necessary in this form, it does not fit like this:
Journal::with('posts')->where('name', 'someValue');

In general, you need to select posts from specific magazines and sort them.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaliy Kravchishin, 2016-04-13
@kirill-93

$posts = Post::with('journal')->whereHas('journal', function ($query) {
    // some query
}])->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question