简体   繁体   English

如何在Laravel模型之间建立正确的关系

[英]How to create a correct relationship between Laravel models

So I have two tables named 'customers' and 'billing_addresses'. 因此,我有两个名为“ customers”和“ billing_addresses”的表。 I also have two models named 'Customer' and 'BillingAddress'. 我也有两个名为“客户”和“ BillingAddress”的模型。

In the billing_addresses table, there are multiple addresses, each having an 'id'. 在billing_addresses表中,有多个地址,每个地址都有一个“ id”。

In the customers table, each customer has an 'billing_address_id' value corresponding to one of the entries in the billing_addresses table. 在客户表中,每个客户都有一个与billing_addresses表中的条目之一相对应的“ billing_address_id”值。

What I managed to create so far is: 到目前为止,我设法创建的是:

// Customer relationship method(in the model file)
   public function address()
{
    return $this->hasOne('App\BillingAddress','id');
}


/////////

// BillingAddress relationship method(in the model file)


 public function customer()
    {
        return $this->hasMany('App\Customer','id');
    }

I am indeed getting the right data when I do something like Customer::with('address'); 当我执行诸如Customer::with('address');类的事情时,我的确获得了正确的数据Customer::with('address');

but I feel like there is a better approach for all this as I'm also getting 但我觉得这是一种更好的方法,因为我也越来越

BadMethodCallException

Call to undefined method 

Illuminate\Database\Eloquent\Relations\HasOne::getForeignKey()  

when I try to filter with datatables(can't tell if its related but first I'd want to have the best approach on the relationships). 当我尝试使用数据表进行过滤时(无法确定其是否相关,但首先我想对关系采用最佳方法)。

Thank you! 谢谢!

A custom can have multiple billing addresses. 一个自定义可以有多个帐单地址。 So it customer can have many addresses and an address belongs to a customer. 因此它的客户可以有许多地址,并且一个地址属于客户。

// Customer Model
public function addresses()
{
    return $this->belongsTo('App\BillingAddress', 'customer_id');
}


// BillingAddress Model
public function customer()
{
    return $this->hasMany('App\Customer', 'billing_address_id');
}

Then you can do : Customer::with('addresses')->get(); 然后,您可以执行以下操作: Customer::with('addresses')->get();

Secondly, make sure you are passing correct foreign key column as second parameter to the relationship methods. 其次,确保将正确的外键列作为第二个参数传递给关系方法。 See documentation for reference 请参阅文档以供参考

The second error is probably causing because of incorrect foreign key column passed as second argument. 第二个错误可能是由于不正确的外键列作为第二个参数传递而引起的。

What you want to achieve is this: 您想要实现的是:

// Customer Model
public function address()
{
    return $this->belongsTo('App\BillingAddress');
}


// BillingAddress Model
public function customer()
{
    return $this->hasMany('App\Customer');
}

You need the belongsTo on your Customer Model because you have the Billing id stored within the Customer table. 您需要在客户模型上使用belongsTo,因为您在客户表中存储了计费ID。 Eloquent will automatically match the billing id to the foreign Key in your customer model. Eloquent会自动将账单ID与客户模型中的外键匹配。 You just need to follow Eloquente naming conventions for foreign keys. 您只需要遵循外来关键字的Eloquente命名约定即可。 Look here for more infos. 在这里查看更多信息。

Eloquent Relations 雄辩的关系

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM