laravel - Eloquent where condition based on a "belongs to" relationship -


let's have following model:

class movie extends eloquent {     public function director()     {         return $this->belongsto('director');     } } 

now i'd fetch movies using condition that's based on column directors table.

is there way achieve this? couldn't find documentation on conditions based on belongs relationship.

you may try (check querying relations on laravel website):

$movies = movie::wherehas('director', function($q) {     $q->where('name', 'great'); })->get(); 

also if reverse query like:

$directorswithmovies = director::with('movies')->where('name', 'great')->get(); // access movies collection $movies = $directorswithmovies->movies; 

for need declare hasmany relationship in director model:

public function movies() {     return $this->hasmany('movie'); } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -