如何在同一张表中拥有两个关联关系

huangapple go评论58阅读模式
英文:

How to have two relationships to the same table

问题

在同一张表中定义两个关系应该如何做?这里有一个简单的例子:

在“Person”模型上,你可以定义以下关系:

public function deals_seller()
{
    return $this->hasMany(Deal::class, 'seller_person_id');
}

public function deals_buyer()
{
    return $this->hasMany(Deal::class, 'buyer_person_id');
}

在“Deal”模型上,你可以尝试定义以下关系:

public function seller()
{
    return $this->belongsTo(Person::class, 'seller_person_id');
}

public function buyer()
{
    return $this->belongsTo(Person::class, 'buyer_person_id');
}

使用Tinker,你可以像这样获取一个人作为卖家或买家的交易:

$person = Person::where('id', 3)->with('deals_seller')->with('deals_buyer')->get();

但是,你无法得到相反的关系:

$deals = Deal::where('amount', '>', 100)->with('buyer')->with('seller')->get();

它不会返回买家或卖家与交易相关的信息。

你应该如何设置这些关系,以便能够检索个别交易或交易列表的买家和卖家呢?

英文:

How should I define two relationships to the same table? Here's a simple example:

Person
 - id
 - name
Deal
 - id
 - seller_person_id
 - buyer_person_id
 - date
 - amount

On the Person model I can have these relationships:

 public function deals_seller()
 {
    return $this->hasMany(Deal::class, 'seller_person_id');
 }

 public function deals_buyer()
 {
    return $this->hasMany(Deal::class, 'buyer_person_id');
 }

On the Deal model I've tried the inverse:

public function seller()
{
    return $this->belongsTo(Person::class, null, 'seller_person_id');
}

public function buyer()
{
    return $this->belongsTo(Person::class, null, 'buyer_person_id');
}

Using tinker, I can get the deals where a Person is a buyer or seller like this:

$person = Person::where('id',3)->with('deals_seller')->with('deals_buyer')->get();

But, I can't get the inverse:

$deals = Deal::where('amount', > , 100)->with('buyer')->with('seller')->get();

It doesn't return the buyer or seller with the deal.

How should I set up the relationships so I can retrieve the buyer and seller for an individual deal or a list of deals?

答案1

得分: 1

在Deal模型上

public function seller()
{
    return $this->belongsTo(Person::class, 'seller_person_id');
}

public function buyer()
{
    return $this->belongsTo(Person::class, 'buyer_person_id');
}

您将$foreign_key参数设置为null。

阅读此处

英文:

On the Deal model

public function seller()
{
    return $this->belongsTo(Person::class, 'seller_person_id');
}

public function buyer()
{
    return $this->belongsTo(Person::class, 'buyer_person_id');
}

You set the $foreign_key parameter to null .

Read this

答案2

得分: 1

以下是翻译好的部分:

public function seller()
{
    return $this->belongsTo(Person::class, 'seller_person_id');
}

public function buyer()
{
    return $this->belongsTo(Person::class, 'buyer_person_id');
}

有了这些额外的关联,您可以像这样检索具有其相应买家和卖家信息的交易列表:

$deals = Deal::where('amount', '>', 100)->with('buyer', 'seller')->get();

这将预加载结果集中每个Deal实例的buyer和seller关系。您可以像这样访问每个交易的买家和卖家信息:

foreach ($deals as $deal) {
    echo "Deal ID: " . $deal->id . "\n";
    echo "Seller: " . $deal->seller->name . "\n";
    echo "Buyer: " . $deal->buyer->name . "\n";
}

希望这对您有所帮助。

英文:

To retrieve the buyer and seller for an individual deal or a list of deals, you can define two additional relationships on the Deal model, each using the belongsTo method to relate to the Person model:

public function seller()
{
    return $this->belongsTo(Person::class, 'seller_person_id');
}

public function buyer()
{
    return $this->belongsTo(Person::class, 'buyer_person_id');
}

With these additional relationships, you can retrieve a list of deals with their respective buyer and seller information like this:

$deals = Deal::where('amount', '>', 100)->with('buyer', 'seller')->get();

This will eager load the buyer and seller relationships for each Deal instance in the resulting collection. You can access the buyer and seller information for each deal like this:

foreach ($deals as $deal) {
    echo "Deal ID: " . $deal->id . "\n";
    echo "Seller: " . $deal->seller->name . "\n";
    echo "Buyer: " . $deal->buyer->name . "\n";
}

huangapple
  • 本文由 发表于 2023年2月24日 04:36:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定