samedi 3 septembre 2016

How to select Model from a Join Query in Laravel

I have a 3 models

  • user
  • helper
  • order

user is connected to helper with a Many-to-Many helper_user pivot table, and connected to orders with Many-to-Many order_user pivot table.

I can easily select helpers's users by

class Helper extends Model {
...
    public function users() {
        return $this->belongsToMany('App\User')
    }
...
}

And I can easily select user's orders by

class User extends Model {
...
    public function orders() {
        return $this->hasMany('App\Order');
    }
...
}

But how do I select helper's orders?

I tried: class Helper extends Model { ... public function orders() { return $this->users()->join('orders', 'users.id', '=', 'orders.user_id')->select('orders.*'); } ... }

This returns orders, but their class is App\User.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire