php - Getting foreign tables value laravel -
here example
table structure
game --id --game posts --id --game_id --post_text class posts extends eloquent { protected $primarykey = 'id'; protected $table = 'posts'; public function games() { return $this->hasone('games','id'); } } class games extends eloquent { protected $primarykey = 'id'; protected $table = 'games'; public function posts() { return $this->belongsto('posts','game_id'); } }
i need game name of post. how can using eloquent?
here initial code
echo $post->games['game'];
but wrong data.
the way queries this.
'query' => string 'select * `games` `games`.`id` = ? limit 1' (length=52) 'bindings' => array (size=1) 0 => int 5
firstly eloquent model names not plural, default should game , post. secondly relationship return values must changed. in hasone , belongsto need use model class names below. left out optional code not required code work.
class post extends eloquent { public function game() { return $this->hasone('game'); } } class game extends eloquent { public function post() { return $this->belongsto('post'); } }
now can game name $post->game->name
Comments
Post a Comment