W
W
WebDev2016-04-13 17:18:42
Laravel
WebDev, 2016-04-13 17:18:42

Condition in eloquent?

Hello, help me add a condition.
There are users, magazines and articles. Journals publish articles, and users read them and can add them to favorites.
Models:

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

The user adds posts to read via belongsToMany (post_user table).
I need to get all user posts by condition and for each post get his log.
If you just get posts, then it works like this:
#
auth()->user()->with(['posts' => function($query) {
        $query->conditions();
}])

But this doesn't work anymore:
#
auth()->user()->with(['posts.entity' => function($query) {
        $query->conditions();
}])

And it’s the conditions that don’t work (this is my scope with conditions for posts)
Because if there are no conditions, then everything is ok:
#
auth()->user()->with('posts.entity')

How do I get posts with their logs and apply conditions to posts?

Answer the question

In order to leave comments, you need to log in

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

auth()->user()->with(['posts' => function($query) {
        $query->with('entity')->conditions();
}])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question