Laravel attach() method not working to hasMany side

前端 未结 4 1489
孤街浪徒
孤街浪徒 2020-12-18 21:34

The application has the models:

Atividade.php

class Atividade extends Eloquent {
    public function interv         


        
4条回答
  •  心在旅途
    2020-12-18 22:32

    See the Laravel documentation here: http://laravel.com/docs/eloquent#inserting-related-models

    Basically you have set up two different types of relationships for the same two tables - you've set up a many-to-many and a one-to-many. It looks as though you probably wanted a many-to-many, so you'll need to change this line:

    return $this->hasMany('Atividade');
    

    To this:

    return $this->belongsToMany('Atividade');
    

    This will set the relationship up as a many-to-many relationship, which will then support the attach() method.

    The attach() method is only for many-to-many, for other relationships there's save() or saveMany() and associate() (see the docs linked above).

提交回复
热议问题