mercredi 28 avril 2021

Obtain customer collection with filtered addresses in Eloquent

I have this two models:

   customers(id,firstname,lastname,phone)
   addresses(id,city,address,customer_id)

With relation:

 class Customer extends Model
 {

     public function addresses()
     {
         return $this->hasMany(Address::class);
     }
 }


 public function customer()
     {
         return $this->belongsTo(Customer::class,'customer_id');
     }

And I'm trying to obtain customers with address in certain city

  return new CustomerCollection(Customer::has('addresses')
                       ->with(array('addresses' => function($q) use ($field)
                        {
                          $q->where('province_id','like',$field)->get();

                        }))->orderBy('id','DESC')->paginate(100));

Result is fine, I obtain correct customers collection, but addresses array is empty. What's wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire