Reputation: 5
I'm trying to display all the Products whose Category matches the $id I'm getting by clicking a specific Category.
I tried using the ->first() and ->pluck('name') functions
I tried doing this in the CategoriesController :
public function show($id)
{
$category = Category::where('id', $id)->pluck('name');
$products = Product::where('categorie', $category)->get();
return view('categories.show')->with('products',$products);
}
ErrorException Method links does not exist. (View: C:\wamp64\www\gestionPointDeVente\resources\views\categories\show.blade.php) (which is the page that displays all the products of that category)
BadMethodCallException Method links does not exist. in Macroable.php line 74
Thanks a lot !
Upvotes: 0
Views: 98
Reputation: 8618
Try this.
public function show($id)
{
$categoryName = Category::where('id', $id)->value('name');
$products = Product::where('categorie', categoryName)->paginate();
return view('categories.show')->with('products',$products);
}
Upvotes: 0
Reputation: 15616
You can use this:
$category = Category::where('id', $id)->first()->name;
->first()
pulls the first item from the database as your entity, then you can use the entity's name
property to access the name.
Upvotes: 1
Reputation: 14268
First of for the Category you could be using route model binding so this :
public function show($id)
Can become this:
public function show(Category $category)
{
// if you decide to keep the id you can uncomment the next line
// $category = Category::find($id);
// the product has the category name or id?
$products = Product::where('categorie', $category->name)->get();
return view('categories.show')->with('products',$products);
}
Then the error says that you are missing a links
method which exists if you use a pagination, but in your code you do not.
So this line:
$products = Product::where('categorie', $category->name)->get();
should become this:
$products = Product::where('categorie', $category->name)->paginate(10);
Upvotes: 1