Laravel 5 使用具有 hasMany 关系的数据透视表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38547060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:12:14  来源:igfitidea点击:

Laravel 5 using pivot table with hasMany relationship

phpmysqllaravellaravel-5eloquent

提问by Hamza AlQabali

I'm making a poll system and I have two tables :

我正在制作一个投票系统,我有两个表:

  • pollstable : Has those fields (id,question,created_at,updated_at).

  • choicestable : Has those fields (id,poll_id,choice).

  • pollstable : 有那些字段 ( id,question,created_at,updated_at)。

  • choicestable : 有那些字段 ( id,poll_id,choice)。

And a pivot table named choice_poll: Has those fields (id,choice_id,poll_id,ip,name,phone, comment ,created_at,updated_at)

还有一个名为的数据透视表choice_poll:具有这些字段 ( id,choice_id,poll_id,ip,name,phone, comment ,created_at,updated_at)

Poll Model :

民意调查模型:

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');
    }
}

Choice Model :

选择型号:

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');
    }
}

Now when I try to build this query it doesn't return the choices :

现在,当我尝试构建此查询时,它不会返回选择:

$poll->first()->choices()->get()

PS:There is many choices in the choices table associated with the first poll.

PS:与第一次投票相关的选择表中有很多选择。

回答by Zakaria Acharki

In this case you have Many To Manyrelationship so try to change belongsToin :

在这种情况下,您有多对多关系,因此请尝试更改belongsTo

public function poll()
{
    return $this->belongsTo('App\Poll')->withPivot('ip','name','phone','comment');
}

To belongsToMany, and it will be :

belongsToMany,它将是:

public function poll()
{
    return $this->belongsToMany('App\Poll')->withPivot('ip','name','phone','comment');
}

NOTE 1:You have to change BelongsToManyto belongsToManynote the Bshould be in lower case.

注意 1:您必须更改BelongsToManybelongsToMany注意B应为小写。

NOTE 2:You want the pivot table with timestapms create_at,updated_atas you mentioned in th OP so you have to use withTimestamps();:

注意 2:您需要create_at,updated_at在 th OP 中提到的带有时间戳的数据透视表, 因此您必须使用withTimestamps();

return $this->belongsToMany('App\Poll')
            ->withPivot('ip','name','phone','comment')
            ->withTimestamps();

//AND in the other side also

return $this->belongsToMany('App\Choice')
            ->withPivot('ip','name','phone','comment')
            ->withTimestamps();

Hope this helps.

希望这可以帮助。

回答by Gabriel Moreira

This:

这个:

public function poll()
{
  return $this->belongsTo('App\Poll')->withPivot('ip','name','phone','comment');
}

and

public function choices()
{
  return $this->BelongsToMany('App\Choice')->withPivot('ip','name','phone','comment');
}

should be:

应该:

public function poll()
{
  return $this->belongsToMany('App\Poll')->withPivot('ip','name','phone','comment');
}

and

public function choices()
{
  return $this->belongsToMany('App\Choice')->withPivot('ip','name','phone','comment');
}