0

I have three model ProductCategory ,Product and ProductPrice . My models are like this

ProductCategory.php

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

And my Product model is like this

 public function category()
    {
        return $this->hasOne('App\Models\ProductCategory');
    }

    public function price(){
       return $this->hasMany('App\Models\ProductPrice');
    }

Now i want get all the product of a category and sort them by name/price as requested. My query for this is

        $query = ProductCategory::with(['product'=>function($q){
          $q->whereHas('price',function ($q){
              $q->where('quantity','>',0);
              $q->where('status',1);
          });

        }])->whereHas('category',function ($q) use ($cat){
            $q->where('category_slug',$cat);
        });

switch ($request->get('sort')){
                case 'price':
             $query->product()->price()->orderBy('amount','asc');
                    break;
                case 'name':
                        $query->product()->orderBy('title','asc');
                    break;

            }
  $data['result']=$query->paginate(30)->toArray();

So i was expecting that this query will return me product from the category and will sort them by price or name as requested. But instead of that its giving me error saying

Call to undefined method Illuminate\Database\Query\Builder::product()

I have followed these threads but no help

Laravel orderBy on a relationship

Laravel Eloquent: How to order results of related models?

https://laracasts.com/discuss/channels/eloquent/order-by-on-relationship

And few more these are the one i found to be close to my problem. Can anyone please help me with the query Thanks

user7747472
  • 1,874
  • 6
  • 36
  • 80
  • You need to either use `->first()` or `->get()` on the end of your database queries otherwise it'll fetch nothing. – Option Apr 10 '18 at 10:10
  • i have that sorry i will update the question – user7747472 Apr 10 '18 at 10:11
  • add this line `$query = $query->first()` before switch statement – Sohel0415 Apr 10 '18 at 10:13
  • This is giving me result but not giving the data of relational tables. It giving data from ProductCategory model only – user7747472 Apr 10 '18 at 10:18
  • 1
    your data model seems a bit off. Why do you have a hasOne on Product to ProductCategory but the ProductCategory belongsTo Product and Category? I assume your ProductCategory table looks like this: `id`| `product_id` | `category_id` ? If that is the case, it would make sense to have `category_id` directly in products table – Matthias S Apr 10 '18 at 10:31
  • first of all you have four models – afsal c Apr 10 '18 at 10:37
  • Product category table is like this `id`| `product_id` | `category_id` | `sub_category_id` | `sub_sub_category_id` – user7747472 Apr 10 '18 at 10:39
  • update your question by all tables data structure – afsal c Apr 10 '18 at 10:41
  • `Category` and `Product` are models here, `ProductCategory` is not a model because it cannot possibly make any sense on its own. Make it a pivot. – apokryfos Apr 10 '18 at 10:52

1 Answers1

0

This problem can not be solved this way as i found out. To solve this problem another approach is necessary .

This is how i did this.

$query = Product::with('sizes','images')->whereHas('productCategory',function ($q) use ($cat,$request,$cate){
            $q->whereHas('category',function ($q) use ($cat ,$request,$cate){
               $q->where('category_slug',$cat);

            });

        });
        $data['result']=$query->get()->toArray();

This can easily be sorted

user7747472
  • 1,874
  • 6
  • 36
  • 80