【发布时间】:2016-11-27 13:46:54
【问题描述】:
我正在制作一个投票系统,我有两张桌子:
polls表:具有这些字段 (id,question,created_at,updated_at)。choices表:具有这些字段 (id,poll_id,choice)。
还有一个名为 choice_poll 的数据透视表:具有这些字段 (id,choice_id,poll_id,ip,name,phone, comment ,created_at,updated_at)
投票模型:
class Poll extends Model
{
protected $table = 'polls';
protected $fillable = ['question'];
public $timestamps = true;
public function choices()
{
return $this->BelongsToMany('App\Choice')->withPivot('ip','name','phone','comment');
}
}
选择模型:
class Choice extends Model
{
protected $table = 'choices';
protected $fillable = ['poll_id','choice'];
public $timestamps = false;
public function poll()
{
return $this->belongsTo('App\Poll')->withPivot('ip','name','phone','comment');
}
}
现在,当我尝试构建此查询时,它不会返回选项:
$poll->first()->choices()->get()
PS:与第一次投票相关的选项表中有很多选项。
【问题讨论】:
标签: php mysql laravel laravel-5 eloquent