lundi 17 février 2020

Get only specific relations records

I have a Client model and Deposits model relation: in the Deposits model table, I have column: campaign_id in the Client model I have:

public function deposits()
{
    return $this->hasMany(ClientDeposit::class, 'client_id');
}

i do simple query:


private function relations()
{
    return [
        'deposits',
    ];
}

$query->with($this->relations());

$query->whereHas('deposits', function ($query) {
   $query->where('campaign_id', '2');
})

this code returns me Clients with 'campaign_id', '2' with but with deposits with all other campaign_id too

clients[0:{deposits: [id: 18507, client_id: 162186, user_id: 22, campaign_id: 2, amount: 100, method_id: null
0: {id: 18507, client_id: 162186, user_id: 22, campaign_id: 2, amount: 100, method_id: null,…}

1: {id: 18505, client_id: 162186, user_id: 22, campaign_id: 2, amount: 100, method_id: null,…}

2: {id: 18506, client_id: 162186, user_id: 22, campaign_id: 10, amount: 100, method_id: null,…}

3: {id: 18498, client_id: 162186, user_id: 22, campaign_id: 3, amount: 100, method_id: null,…}}]

I want that the relation in the Client record will return only Deposits with campaign_id 2 an not all other related



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire