У меня также есть другая страница для отображения удаленной категории (конечно, без продуктов). Но когда я попытался получить удаленную категорию, выдала ошибку
Вызов функции-члена Products() по нулевому значению
Вот код:
Модели
Код: Выделить всё
// CategoryModel
public function products()
{
return $this->hasMany(Product::class, 'category_id','id');
}
// ProductModel
public function category()
{
return $this->belongsTo(Categories::class, 'category_id','id')->withTrashed();
}
Код: Выделить всё
//IndexController (Here I display all of the products based on non-trashed category for public access)
public function productByCategory($name)
{
$categories = Categories::where('name', $name)->first();
$productsCategories = $categories->products()->get(); // the error occurs here
return view('products-category', compact('productsCategories','categories'));
}
//CategoryController (Here I display all of the trashed categories, without the product, for admin access)
public function trash()
{
$categories = Categories::onlyTrashed()->paginate(10);
return view("categories. Trash", compact('categories'));
}
//ProductController (Here I display all of the products for admin access)
public function trash()
{
$products = Product::onlyTrashed()->paginate(10);
return view("products.trash", compact('products'));
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... rent-blade