Я хочу редиректить пользователя назад, если он нажмет на категорию, в которой нет постов. Я пытался делать так, но что то не выходит:
$check = Post::all();
if($check->count() < 1){
return redirect()->back()->with('warning', 'No posts in this category!');
}
$check = Category::withCount('posts')->first();
if($cheсk->posts_count < 1){
return redirect()->back()->with('warning', 'No posts in this category!');
}
if(Category::withCount('posts') < 1){
return redirect()->back()->with('warning', 'No posts in this category!');
}
Category model:
class Category extends Model
{
use HasFactory;
protected $fillable = [
'name',
'user_id',
'code',
'img'
];
public function posts(){
return $this->hasMany(Post::class);
}
}
Кто-нибудь знает как это сделать правильно?