/** * return all categories that this post * do not belongs to. * * @return Category */ public function CategoriesNotIn() { $category_ids = $this->categories->map(function ($category) { return $category->id; }); return Category::whereNotIn('id', $category_ids)->get(); }
/** * Run the database seeds. * * @return void */ public function run() { $posts = Post::all(); $categories = Category::all(); foreach ($categories as $category) { $subPosts = $posts->take(3); foreach ($subPosts as $post) { $post->addToCategory($category); } } }