0

) I have method:

public function show($id){
$categories = Category::findOrFail($id);
$productsList = Category::with(['products' => function ($query) {
                  $query->where(['category_id', 19]);
              }])->get();
return view('categories.showcategory', compact('categories', 'productsList'));
}

This method returned a list of products in the selected category. Number 19 is selected ID category. The URL to list of products in the selected category looks like this: www.[...]magazyn_michal/public/addcategory/19 The question is: How can I pass a dynamic value numeric ID category (19) of the URL to the method ?? I try this (but not work):

public function show($id){
$categories = Category::findOrFail($id);
$productsList = Category::with(['products' => function ($query) {
                  $query->where(['category_id' => $categories->id]);
              }])->get();
return view('categories.showcategory', compact('categories', 'productsList'));
}

Laravel returned:

Undefined variable: categories

This way also does not work:

public function show($id){
$categories = Category::findOrFail($id);
$productsList = Category::with(['products' => function ($query) {
                  $query->where('category_id', Input::get('category_id'));
              }])->get();
return view('categories.showcategory', compact('categories', 'productsList'));
}

Laravel nothing does not return. Empty list of products in the selected category.

routes.php file:

Route::get('/', 'ProductsController@index');
Route::get('/contact', 'PagesController@contact');
Route::resource('/addarticle', 'ArticlesController');
Route::resource('/addcategory', 'CategoriesController');
Route::resource('/listcategory', 'CategoriesController@listCategory');
Route::resource('/warehouse', 'ProductsController');
Route::auth();
Route::get('/home', 'HomeController@index');

model category.php:

public function products(){
    return $this->hasMany('App\Product');
}

model product.php:

public function category(){
    return $this->belongsTo('App\Category');
}

I using this: https://laravel.com/docs/5.2/eloquent-relationships#constraining-eager-loads

major697
  • 139
  • 1
  • 2
  • 15
  • 1
    You need to post your route so we can see how it is setup – Rob Fonseca Jul 03 '16 at 23:57
  • @Rob Fonseca I update my post – major697 Jul 04 '16 at 06:58
  • Have you dd($categories) in your controller to make sure it is actually pulling one out of the database? Nothing seems wrong with your route, method definition and pulling the category out of the model, so want to make sure you're actually getting a category first. – Rob Fonseca Jul 04 '16 at 11:04
  • @RobFonseca `$categories = Category::findOrFail($id); dd($categories);` returned this: [link](http://iv.pl/images/88817936389407369545.png) – major697 Jul 04 '16 at 11:53

3 Answers3

1

You should be using route-model binding in this situation. You can have your route /categories/19 return the Category object instead of just an ID. This could be already happening, causing errors when you call findOrFail.

Check out: https://laravel.com/docs/master/routing#route-model-binding

You should also use Laravel relationships, so you can put this function in your Category class:

public function products(){
  return $this->belongsToMany('App\Product');
}

More reading on relationships: https://laravel.com/docs/master/eloquent-relationships

If you do both of these, your controller method will look like this:

public function show(Category $category){
  $products = $category->products;
  return view('categories.showcategory', compact('category', 'products'));
}
Jeff
  • 24,623
  • 4
  • 69
  • 78
  • You don't need to use a sub-query to locate the products. The relationship automatically looks for products with the correct `category_id`, so you can just do `$productList = $category->products;`. Also, please look at the documentation for route-model binding, the section on "Explicit Binding" is what you need to do to get the `Category` object instead of the ID. – Jeff Jul 04 '16 at 12:36
1

I think I have it based on all the test you have shown me. $categories is out of scope in your query. Try this:

public function show($id){
$categories = Category::findOrFail($id);
$productsList = Category::with(['products' => function ($query) use ($categories) {
                  $query->where('category_id', $categories->id)
              }])->get();
return view('categories.showcategory', compact('categories', 'productsList'));
}

The use declaration puts categories in scope of your anonymous function

Rob Fonseca
  • 3,671
  • 2
  • 17
  • 13
0

OK I repair this problem. Now it's working ;)

public function show($id){
$categories = Category::findOrFail($id);
$categoryID = Input::get('category_id');
$productsList = Category::with(['products' => function ($query) use($id) {
                  $query->where('category_id', '='  ,$id);
              }])->get();
return view('categories.showcategory', compact('categories', 'productsList'));
}
major697
  • 139
  • 1
  • 2
  • 15