mile-panic
mile-panic

Reputation: 135

Laravel: How to show all posts which category is not blocked?

I have models User, Category and Post. Post belongs to many users and many categories. User and Category are not related. I created a custom pivot table in which i store user_id and category_id, and a model CategoryBlock for that table. How can I get all posts which category the user didn't block?

Upvotes: 1

Views: 687

Answers (1)

CodexVarg
CodexVarg

Reputation: 510

easiest way is to get all block categories and then get post where not in this collection

EDIT

$block = DB::table('category_block')->where('user_id', Auth::user()->id)->get()->pluck(['category_id'])->toArray();

$posts = Post::whereNotIn('category_id', $block)->get();

Upvotes: 1

Related Questions