php - Using Joins in Eloquent -


i'm relative beginner in laravel 4 , i'm having trouble retrieving data joined table defined in model. tried following directions in laravel's documentation here , here.

here's did: have model (car.php) retrieves data cars table:

class car extends eloquent{     public function parts(){         return $this->hasmany('carparts');     } } 

here's carparts model (car-parts.php):

class carparts extends eloquent{     protected $table = 'car_parts';     public function car(){         return $this->hasone('car');     } } 

i tried these data in joined table:

echo car::find(1)->parts; // tried: $cars = car::find(1)->get(); echo $cars->parts; 

but error message: undefined property:car::$parts

the best way use dynamic property (->parts) should work you:

$car = car::find(1);  echo $car->parts; 

or

$car = car::find(1);  foreach($car->parts $part) {     echo $part->name; } 

the parts() method gives query object, may used filter little more parts:

$car = car::find(1);  $parts = $car->parts()->where('model', '=', 'ford')->get();  foreach($parts $part) {     echo $part->name; } 

also have change carparts relation to:

$this->belongsto('car'); 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -