简体   繁体   English

与Laravel一起使用Eloquent的ORM的最佳方法?

[英]Best way to apply Eloquent's ORM with Laravel?

I have two tables: 我有两个表:

treatments (
id,
name
)

companies (
id,
name
)

And I need to build a relation to a "price" table. 我需要建立与“价格”表的关系。 I thougth in something like follows: 我想到以下内容:

prices (
treatment_id,
company_id,
price
)

But i don know how to apply the ORM to a php aplication. 但是我不知道如何将ORM应用于php应用程序。 I'm using Laravel with Eloguent's ORM. 我正在将Laravel与Eloguent的ORM一起使用。 I think that the real question would be if this is a good way to design the db. 我认为真正的问题是这是否是设计数据库的好方法。 Perhaps I should make it diferent? 也许我应该使其与众不同? Any advices? 有什么建议吗? Thanks, Ban. 谢谢,班

If a Company can have multiple Treatments and a treatment can be bought from multiple companies at different prices, then you have a Many-to-many relationship, with prices being the pivot table (which if you would adhere to convention would be named company_treament , but that's not a must). 如果一家公司可以有多种治疗,并且可以以不同的价格从多个公司购买治疗,那么您将具有多对多关系,而prices是数据透视表(如果您遵守约定,则将其命名为company_treament ,但这不是必须的)。 So you'll need to have two models for Treatments and Companies , which would look like this: 因此,您需要为TreatmentsCompanies建立两个模型,如下所示:

class Company extends \Eloquent {

    public function treatments()
    {
        return $this->belongsToMany('Treatment', 'prices')->withPivot('price');
    }

and

class Treatment extends \Eloquent {

    public function companies()
    {
        return $this->belongsToMany('Company', 'prices')->withPivot('price');
    }
}

The treatments() and companies() methods from the models are responsible for fetching the related items. 模型中的treatments()companies()方法负责获取相关项目。 Usually the hasMany method only requires the related model as the first parameter, but in your case the pivot table name is non-standard and is set to prices by passing it as the second parameter. 通常, hasMany方法仅将相关模型作为第一个参数,但是在您的情况下,数据透视表名称是非标准的,并且通过将其作为第二个参数传递而设置为prices Also normally for the pivot table only the relation columns would be fetched ( treatment_id and company_id ) so you need to specify the the extra column using withPivot . 同样通常对于数据透视表,仅会提取关系列( treatment_idcompany_id ),因此您需要使用withPivot指定额外的列。 So if you want to get the treatments for a company with the id 1 list you whould to something like this: 因此,如果您想获得一家ID为1的公司的待遇,您应该这样做:

$treatments = Company::find(1)->treatments;

The opposite is also true: 反之亦然:

$companies = Treatment::find(1)->companies;

If you need to access the price for any of those relations you can do it like this: 如果您需要访问任何这些关系的价格,可以这样做:

foreach ($treatments as $treatment)
{
    $price = $treatment->pivot->price;
}

You can read more about how to implement relationships using Eloquent in the Laravel Docs . 您可以在Laravel文档中阅读有关如何使用Eloquent实现关系的更多信息。

EDIT 编辑

To insert a relation entry in the pivot table you can use attach and to remove one use detach (for more info read the Docs ). 要在数据透视表中插入一个关系条目,可以使用attach并使用detach删除一个(有关更多信息,请阅读Docs )。

$treatment->companies()->attach($companyId, array('price' => $price));
$treatment->companies()->detach($companyId);

To update a pivot table entry use updateExistingPivot : 要更新数据透视表条目,请使用updateExistingPivot

$treatment->companies()->updateExistingPivot($companyId, array('price' => $price));

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

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