dimanche 30 juin 2019

Select specific column value from multiple relational table using where condition in Laravel ORM

I have two tables named contacts and clients. Both tables have group_id as foreign_key. Now, I want to get phonecolumn value from both tables when user $request->groupid will found a group from groupstable. I am trying something like this. But getting empty array. Would someone help me please!

$getPhoneNumbers = Group::with(['hasContacts' => function($query){
                             $query->select('phone')->where('is_active', 1);
                        }])->with(['clients' => function($q){
                             $q->select('phone')->where('status', 1);
                        }])->where('id', $request->groupid)->get();

In Group model -

public function clients()
{
    return $this->hasMany('App\Client', 'group_id', 'id');
}

public function hasContacts()
{
    return $this->hasMany('App\Contact', 'group_id', 'id');
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2XiO0QL
via IFTTT

Aucun commentaire:

Enregistrer un commentaire