A post shouldn't be saved if it doesnt belong to any category. How to approach that?

Posted 2 years ago by vjolenz

// in Post model

public function categories() { return $this->belongsToMany(Category::class, 'posts_categories', 'app_id', 'category_id'); }

//in Category Model

public function posts() { return $this->belongsToMany('App\Post', 'posts_categories', 'category_id', 'app_id'); }

I want to be sure that a post can't be saved if it doesnt belong to any category. How to approach that?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.