通过" Parent"检索远程相关的模型。模型(后裔模型?)

时间:2016-11-09 15:02:27

标签: laravel-5 eloquent

我的模型有以下情况。

  • 用户有很多公司。
  • 公司有很多客户。
  • 客户有很多发票。
  • 发票有很多交易。

我想要达到的目标是,我可以拨打那些 $ user->客户 $ user->发票 $ user-&gt ;用户模型上的;事务 $ user->客户正在使用 hasManyThrough 关系,因为客户表格中有 company_id 字段然后转到公司表,其中存在 user_id 字段,然后转到用户表并检查 id 领域。这非常类似于客户 - >公司 - >用户,这意味着它从每个客户转到公司,然后转到公司所有者(用户)。这意味着我希望发票 - >客户 - >公司 - >用户交易 - >发票 - >客户 - >公司 - >用户

现在发票交易表格没有 user_id company_id 字段,这意味着据我所知,我不能只输入 hasManyThrough 。目前,我正在逐个检索发票交易,并将它们存储在我返回的集合中。

所以我的问题是弄清楚如何从所有发票中回溯以找到所有者(用户模型),这需要转发给客户的发票,从客户到公司,而不是从公司到用户

invoices - customer_id (Go to the Customers table)
    customers - company_id (Continue to the Companies table)
        companies - user_id (Continue to the Users table)
            users - id (This should now return all invoices)

transactions - invoice_id (Go to the Invoices table)
    invoices - customer_id (Continue to the Customers table)
        customers - company_id (Continue to the Companies table)
            companies - user_id (Continue to the Users table)
                users - id (This should now return all transactions)

所以我想要的是从公司模型中获取某些类型的所有模型,并返回它们的Eloquent Collection,以便能够对它们进行分页或进一步处理。

这是一个模型,让您了解我目前正在做什么。

<?php

namespace App;

class User extends Eloquent
{
    // Companies Table
    // - id
    // - user_id
    // ...
    public function companies()
    {
        return $this->hasMany(Company::class);
    }

    // Customers Table
    // - id
    // - company_id
    // ...
    public function customers()
    {
        return $this->hasManyThrough(Customer::class, Company::class);
    }

    // Invoices Table
    // - id
    // - customer_id
    // ...
    public function invoices()
    {
        $invoices = new collect([]);

        foreach ($this->customers as $customer) {
            $invoices = $invoices->merge($customer->invoices);
        }

        return $invoices;
    }

    // Transactions Table
    // - id
    // - invoice_id
    // ...
    public function transactions()
    {
        $transactions = collect([]);

        foreach ($this->invoices() as $invoice) {
            $transactions->push($invoice->transaction);
        }

        return $transactions;
    }
}

1 个答案:

答案 0 :(得分:0)

可以使用belongsTo关系轻松完成这些操作。

这些应该允许您能够$transaction->user或中间的任何关系

class Transaction extends Model
{
    public function invoice()
    {
        return $this->belongsTo(Invoice::class);
    }

    public function customer()
    {
        return $this->invoice->customer();
    }

    public function company()
    {
        return $this->customer->company();
    }

    public function user() 
    {
        return $this->company->user();
    }
}

class Invoice extends Model
{
    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    public function company()
    {
        return $this->customer->company();
    }

    public function user()
    {
        return $this->company->user();
    }
}

class Customer extends Model
{
    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    public function user()
    {
        return $this->company->user();
    }
}

class Company extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
相关问题