php 无法让 Laravel 助理工作

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

Can't get Laravel associate to work

phplaravelrelational-databaseeloquentrelationship

提问by Matthijn

I'm not quite sure if I understand the associatemethod in Laravel. I understand the idea, but I can't seem to get it to work.

我不太确定我是否理解Laravel 中的关联方法。我理解这个想法,但我似乎无法让它发挥作用。

With this (distilled) code:

使用此(蒸馏)代码:

class User
{

    public function customer()
    {
        return $this->hasOne('Customer');
    }

}

class Customer
{

    public function user()
    {
        return $this->belongsTo('User');
    }

}

$user = new User($data);
$customer = new Customer($customerData);

$user->customer()->associate($customer);

I get a Call to undefined method Illuminate\Database\Query\Builder::associate()when I try to run this.

Call to undefined method Illuminate\Database\Query\Builder::associate()当我尝试运行它时,我得到了一个。

From what I can read, I do it exactly as is stated in the docs.

从我可以阅读的内容来看,我完全按照文档中的说明进行操作。

What am I doing wrong?

我究竟做错了什么?

回答by David Barker

I have to admit that when I first started using Laravel the relationships where the part that I had to consistently refer back to the docs for and even then in some cases I didn't quite get it right.

我不得不承认,当我第一次开始使用 Laravel 时,我必须始终参考文档的部分的关系,即使在某些情况下我也没有完全正确。

To help you along, associate()is used to update a belongsTo()relationship. Looking at your code, the returned class from $user->customer()is a hasOnerelationship class and will not have the associate method on it.

助你一臂之力,associate()用于更新一段belongsTo()关系。查看您的代码,从中返回的类$user->customer()是一个hasOne关系类,并且上面没有关联方法。

If you were to do it the other way round.

如果你反过来做。

$user = new User($data);
$customer = new Customer($customerData);

$customer->user()->associate($user);
$customer->save();

It would work as $customer->user()is a belongsTorelationship.

它的工作作为$customer->user()一个belongsTo关系。

To do this the other way round you would first save the user model and then save the customer model to it like:

要以相反的方式执行此操作,您首先要保存用户模型,然后将客户模型保存到其中,例如:

$user = new User($data);
$user->save();

$customer = new Customer($customerData);
$user->customer()->save($customer);

Edit: It may not be necessary to save the user model first but I've just always done that, not sure why.

编辑:可能没有必要先保存用户模型,但我一直这样做,不知道为什么。

回答by alexrussell

As I understand it, ->associate()can onyl be called on a BelongsTo relationship. So, in your example, you coulddo $customer->user()->associate($user). However, in order to 'associate' a Has* relationship you use ->save(), so your code should be $user->customer()->save($customer)

据我了解,->associate()可以在 BelongsTo 关系上调用 onyl。因此,在您的示例中,您可以执行$customer->user()->associate($user). 但是,为了“关联”您使用的 Has* 关系->save(),因此您的代码应该是$user->customer()->save($customer)