0

I'm new to Laravel so I have a bit of a problem. I have tables 'Categories' and 'Products' in my DB. In my models I setup the relations like:

Product.php:

public function category() 
{
    return $this->belongsToMany(Category::class);
}

Category.php:

public function products() 
{
    return $this->hasMany(Product::class);
}

What I need now is that I want to select all of the Categories with their related Products. (User enters in search bar category name and gets list of Categories and when user selects Category I get all of the columns from Categories and ALSO Products related with this Category).

I have tried something like this:

public function findCategory(Request $request)
{
    return Category::with('products')
        ->where('name', 'like', '%' . $request->category_name . '%')
        ->limit(15)
        ->get();
}

ALSO:

public function findCategory(Request $request)
{
    return Category::where('name', 'like', '%' . $request->category_name . '%')
        ->products()
        ->limit(15)
        ->get();
}

But this doesn't seem to work and I ran out of ideas. Does anyone know if there is a way to do this? Any help would be much appreciated :)

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
ineffable
  • 169
  • 1
  • 5
  • 15
  • in product collection are you searching with where or from category? – Ritesh Khatri Nov 16 '17 at 11:24
  • Not sure if I understood your question. But I don't need ' where' in products collection. The where clause should be on category. Basically like Category::where('name', 'like', '%' . $request->category_name . '%') and this works just fine, but i also want to get products related to specific category – ineffable Nov 16 '17 at 11:29
  • is there a many to many relation? – Ritesh Khatri Nov 16 '17 at 11:30
  • yes, right now i get to my vue component data- list of categories (in which i have columns like id, name etc) and i also want an array of products which are related to this specific category – ineffable Nov 16 '17 at 11:36
  • see this i hope it help you -> [link](https://stackoverflow.com/questions/36208460/hasmany-vs-belongstomany-in-laravel-5-x) – Ritesh Khatri Nov 16 '17 at 11:54
  • ok, changed to belongsToMany in Product, and I do have a pivot table called product_category with foreign keys 'id' (which is a product id) and category_id, also i created a model ProductCategory (not sure what relations should be there), and I have a ProductCategoriesController in which i have this method findCategory – ineffable Nov 16 '17 at 12:01
  • hmm now what happen? – Ritesh Khatri Nov 16 '17 at 12:11
  • for many to many relation for the parent `belongsToMany` as well as for child we have to use `belongsToMany`. – Ritesh Khatri Nov 16 '17 at 12:14
  • If you need reference go through documentation [link](https://laravel.com/docs/5.5/eloquent-relationships) Many to Many topic. – Ritesh Khatri Nov 16 '17 at 12:15
  • nothing happened, unfortunately it still doesn't work, even though i changed it. I used solution below but still the same error "Error: Request failed with status code 500" – ineffable Nov 16 '17 at 12:16
  • every error has solution, output is our primary goal, :) – Ritesh Khatri Nov 16 '17 at 12:39
  • I also searching some stuff for you brother to point you exactly the concept of this relation. – Ritesh Khatri Nov 16 '17 at 12:40
  • Check the answer I gave. You were missing `with` function. – Oluwatobi Samuel Omisakin Nov 16 '17 at 13:47

3 Answers3

0

it would be something like

public function findCategory(Request $request)
{

    $term = $request->category_name;

    return Category::with('products')->where(function($q) use($term) {
               $q->where('categories.name','like',"%$term%");
            })->paginate(15); //or use limit(15)


}
aimme
  • 6,385
  • 7
  • 48
  • 65
  • Oh but wait, doesn't the where clause search in relation? (where is executed on products instead of categories?) Cause, unfortunately, it doesn't seem to work. I'm using axios to get results (in my vuejs component) but it just responds with "Uncaught (in promise) Error: Request failed with status code 500" :( – ineffable Nov 16 '17 at 11:25
  • oh i am sorry. mistyped it. check now. – aimme Nov 16 '17 at 11:50
0

Your query is almost complete except that you didn't load related model. Therefore, you last example should be:

public function findCategory(Request $request)
{
    return Category::where('name', 'like', '%' . $request->category_name . '%')->
      ->with('products') //load related products
      ->limit(15)
      ->get();
}
0
public function getAllData()
{
    $data = tableName::where('Method','Delivery')->get();
    return response()->json($data, 200);
}

OR

$users = DB::table_Name('users')->select('name', 'email as user_email')->get();
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – mufazmi May 21 '21 at 18:16