jeudi 22 novembre 2018

Eloquent eager load constrain by parent column values not working

I have this following tables;

shipment
==========
date document_no address_id
----------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0


item
=======
shipment_date document_no address_id
-------------------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0

and below is the Models;

class Shipment extends Model {

    protected $table = 'shipment';

    public function items()
    {
        return $this->hasMany(Item::class, 'document_no', 'document_no')
            ->where(['shipment_date' => $this->date, 'address_id' => $this->address_id]);
    }
}

class Item extends Model {

    protected $table = 'item';
}

I tried to get the data for API;

return Shipment::with('items')->where('date', $shipment_date)->get()->toArray();

but I am getting empty items. But weirdly I am getting the items data when I am using this; Shipment::find($id)->items. What did I miss?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire