Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anand_aks's avatar

How to get data from multiple relationship function ?

The modal name is Client_user, which has multiple relationship to other tables, Roles, Clients, Kds the functions are as follows

class Client_user extends Authenticatable
{
	public function roles()
	{
        return $this->belongsToMany(Role::class);
    }

    public function clients()
    {
        return $this->belongsToMany(Client::class);
    }

	public function kds()
    {
        return $this->belongsToMany(Kds_master::class);
    }
}

For getting the relation data for eg: say kds what I usually do is

$data = Client_user::with('kds')->get();

Is there any way to get all the related data together in a single function like?

	public function clientapi()
    {
        return $this->belongsToMany(Role::class,Client::class,Kds::class);
    }

It doesn't work. My question is anything similar to this, without 3 functions

0 likes
2 replies
Sinnbeck's avatar

No those a different types of data, so getting them in a single list would make no sense. A role is not the same as a Client, and is not the same as a Kds.

But you can do

$data = Client_user::with(['roles', 'clients', 'kds'])->get();
1 like
anand_aks's avatar

@Sinnbeck

Thanks, I have one more doubt

if 1 relationship has a parameter say company

public function clients($company)
    {
        return $this->belongsToMany(Client::class)->where('company',$company);
    }

How do I pass it when we use like below

$data = Client_user::with(['roles', 'clients', 'kds'])->get();
// how to pass the company parameter to the clients

Please or to participate in this conversation.