mardi 24 avril 2018

How to filter data from relational table in laravel

I have three table one is orders table and another is order_status table and another is status table. Purpose of order_status table is to keep a track order's events. My table has the following column.

Order table

----------------------------
id | ref_num | name | email |
-----------------------------

Order status table has

---------------------------
order_id | status_id
---------------------

My models are like this Order model

 public function orderStatus(){
        return $this->hasMany(OrderStatus::class');
    }

Order status model

  public function detail(){
        return $this->belongsTo(Status::class,'status_id','id');
    }

    public function order(){
        return $this->belongsTo(Order::class);
    }

Now i want to get all those order which are still pending. How can i do so ?

I tried to retrive like this but it failed

 $data['orders']= Order::with(['orderStatus' =>function($q){
                $q->with('detail')->latest()->where('status_id',2);
            }])->latest()->take(10)->get()->toArray();

This return only one after that it does not.

Can anyone please tell me how can i sort this one ?? Thanks

PS:: one order can have multiple status such as unpaid, pending, packageging, in transit and so on but in sequence ofcouse



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire