php Laravel attach() 方法不适用于 hasMany 方

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21566705/
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-08-25 04:15:30  来源:igfitidea点击:

Laravel attach() method not working to hasMany side

phplaravel-4

提问by Bledson

The application has the models:

该应用程序具有以下模型:

Atividade.php

抗疫网

class Atividade extends Eloquent {
    public function intervencoes() {
        return $this->belongsToMany('Intervencao');
    }
}



Intervencao.php

干预.php

class Intervencao extends Eloquent {
    public function atividades() {
        return $this->hasMany('Atividade');
    }
}



The following code works:

以下代码有效:

Atividade::find($id)->intervencoes()->attach($intervencao_id);

But, this...

但是这个...

Intervencao::find($id)->atividades()->attach($atividade_id);

Returns an BadMethodCallException:

返回一个 BadMethodCallException:

Call to undefined method Illuminate\Database\Query\Builder::attach()

调用未定义的方法 Illuminate\Database\Query\Builder::attach()



SOLUTION (thanks to @gnack):

解决方案(感谢@gnack):

I was trying to set a many-to-many relationship, so just needed to change this...

我试图建立一个多对多的关系,所以只需要改变这个......

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

To this:

对此:

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

回答by Nick Coad

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

请参阅此处的 Laravel 文档: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.

这会将关系设置为多对多关系,然后支持该attach()方法。

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

attach()方法仅适用于多对多,其他关系有save()orsaveMany()associate()(请参阅上面链接的文档)。

回答by warspite

attach()is for many-to-many relationships. It seems your relationship is supposed to be a many-to-many but you have not set it up correctly for that.

attach()用于多对多关系。看起来你们的关系应该是多对多的,但你没有为此正确设置。

class Intervencao extends Eloquent {
    public function atividades() {
        return $this->belongsToMany('Atividade');
    }
}

Then the attach()should work

然后attach()应该工作

回答by UserHelpNeeding02356

See the documentation Laravel 5.7

请参阅文档 Laravel 5.7

A Comment belongTo an unique Post

评论属于一个独特的帖子

class Comment extends Model
{
    /**
     * Get the post that owns the comment.
     */
    public function post()
    {
        return $this->belongsTo('App\Post');
    }
}

A Post can Have multiple Comments

一个帖子可以有多个评论

class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
        return $this->hasMany('App\Comment');
    }

When you want to update/delete a belongsTo relationship, you may use the associate/dissociate method.

当你想更新/删除一个belongsTo关系时,你可以使用associate/dissociate方法。

$post= App\Post::find(10);
$comment= App\Comment::find(3);
$comment->post()->associate($post); //update the model

$comment->save(); //you have to call save() method

//delete operation
$comment->post()->dissociate(); 

$comment->save(); //save() method