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

mattdj's avatar

Get date from multiple table

Hello,

my tables:

user --> normal table

orders -> with user_id,

rates -> with user_id, orders_id ;

In model Rate:

public function UserRate()
    {
        return $this->belongsTo('App\User');
    }

    public function GetOrder()
    {
        return $this->hasOne('App\Order');
    }
}

In model user:

public function orders()
    {
        return $this->hasMany('App\Order');
    }
    public function offers()
    {
        return $this->hasMany('App\Offer');
    }
    public function rates()
    {
        return $this->hasMany('App\Rate');
    }

Question: How to get order title(in order table) from RateController. Order title belong to specyfic order and user id.

I just one to have: order with user name(user table) and order title(from title table).

I try like this:

 $user = User::findOrFail($id);
        $rates = $user->rates->toArray();   
        $result = Rate::with($rates)->getOrder()->toArray();

but it fails.

Thx in advence. I love this forum.

0 likes
2 replies
pixelpeter's avatar

Without testing I assume it could be something like this

$user = User::with(['orders', 'rates'])->whereId($userID)->get();
1 like
mattdj's avatar

@pixelpeter That works but I want to have connection between orders and rates: orders.id = rates.order_id

Should return only one match, now I have a lot . ( that why I have $this->hasOne in model)

Please or to participate in this conversation.