vendredi 23 mars 2018

Eloquent query to list items visibility constraints

I'm struggling with an Eloquent query. My end goal is to list all public posts authored by anyone and also the private ones that were authored by a given user from a given publication.

Example:

Given these 3 posts

title: Title 1 | user_id: 1 | is_private: true

title: Title 2 | user_id: 2 | is_private: false

title: Title 3 | user_id: 3 | is_private: true

The output for userId 1 should be:

Title 1 and Title 2

My best shot at it was this the following:

    $posts =  Post::where('publication_id', $publicationId)
        ->where('is_private', false)
        ->where(function ($query) {
            $query->where('owner_id', auth()->id())
                ->where('is_private', true);
        });

Which I know it's far from what is need.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire