samedi 23 juillet 2016

Laravel 5 using pivot table with hasMany relationship

im making a poll system and i have 2 tables :

polls table with those fields : id,question,created_at,updated_at

choices table with those fields : id,poll_id,choice

and a pivote table named "choice_poll" with those fields : id,choice_id,poll_id,ip,name,phone, comment ,created_at,updated_at

this is my models :

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

and

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 .



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2aiSVYH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire