jeudi 21 février 2019

how to get none related objects of Many to Many relationship

I have user and news table and then a middle table called `news_user, the middle table determines which news has been seen by the user. I can easily get objects that have been seen but, but now I need to show the user objects that have not been seen.

For this, I did a workaround but I don't consider it as the healthiest solution.

This is what I did:

$seenNewses = DB::table('news_user')->where('user_id', Auth::id())
->pluck('news_id')->toArray();

$notSeenNewses = News::whereNotIn('id', $seenKeepers)
->orderBy('id', 'asc')->first();

Is there a way I can do this with a single query?

P.S: I have seen similar questions but they got little confused. Any answer is appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire