laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.51k stars 11.02k forks source link

Query directly with objects #6957

Closed Exalyon closed 9 years ago

Exalyon commented 9 years ago

I am currently using Laravel 4.2. I have a Order belonging to a User :

class Order extend Eloquent 
{
    public function user () 
    {
         return $this->belongTo('User');
    }
}

And a User hasMany Order :

class User extend Eloquent 
{
    public function orders () 
    {
         return $this->hasMany('Order');
    }
}

I am able to do $user->orders to get the orders related to a user but I was wondering if it was possible to get the same result the other way around direcly with objects like in other ORMs. Something like Order::where('user', '=', User::find(1)).

GrahamCampbell commented 9 years ago

You could do Order::where('user', '=', User::find(1)->id) or something like that. Please ask question like this on the forums.