Thursday, April 13, 2017

Return relationship only if user is logged in?

I have a Posts and a Comments table, where each post can have multiple comments.

I want to make a query that gets all posts, and also all of the logged in user's comments.

Here is what I have so far:

$posts = Post::select('posts.*')
    ->with(['comments' => function($query) {
        if (Auth::check()) {
            $query->where('user_id', Auth::user()->id);
        }
    }])
    ->get();

And my Post.php model class looks like this:

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}

The query returns the correct results when the user is logged in.

But when the user is not logged in, it returns the comments of ALL users, instead of returning nothing (because the user is logged in and therefore they have no comments).

How can I fix this?



via user7863944

Advertisement