Laravel 5 hasManyThrough

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

Laravel 5 hasManyThrough

phplaravel

提问by Liam

I have 3 tables: company<-> users<-> invoice.

我有 3 个表:company<-> users<-> invoice

A company hasManyusers.

某公司hasMany用户。

A user belongsToa company and, and a user hasManyinvoices.

一个用户belongsTo一个公司和一个用户hasMany发票。

An invoice belongsToa user.

belongsTo一个用户的发票。

Now I have an invoice with information about user (customer), and I want to get the user its information about the company so I made an:

现在我有一张包含用户(客户)信息的发票,我想获取用户关于公司的信息,所以我做了一个:

An invoice hasManyThroughusers, company (so gets the company through user)

发票hasManyThrough用户,公司(因此通过用户获取公司)

Now it doesn't work as it is needed.

现在它不工作,因为它需要。

Models:

楷模:

class Company extends Eloquent {

    protected $table = 'companies';

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

    public function invoices()
    {
        return $this->hasManyThrough('App\Company', 'App\User');
    }
}

class User extends Model {

    protected $table = 'users';

    public function usertype()
    {
        return $this->belongsTo('App\UserType','usertype_id','id');
    }

    public function company()
    {
        return $this->belongsTo('App\Company','company_id','id');
    }

    public function invoice()
    {
        return $this->hasMany('App\Invoice');
    }

}

class Invoice extends Model {

    protected $table = 'invoices';

    public function users() {
        return $this->belongsTo('App\User', 'id');
    }
}

Invoice Controller:

发票控制员:

class InvoiceController extends Controller {

    private $invoice;

    public function __construct(Invoice $invoice)
    {
        $this->invoice = $invoice;
    }

    public function index(Invoice $invoice)
    {
        $invoices = $invoice->with('users', 'company')->get();

        dd($invoices);

        return view('invoice.index', compact('invoices'));
    }

    public function create()
    {
        //
    }

    public function store()
    {

    }

    public function show($id)
    {
        $invoice = Invoice::with('users')->find($id);

        return view('invoice.show', compact('invoice'));
    }

    public function edit($id)
    {
        //
    }

    public function update($id)
    {
        //
    }

    public function destroy($id)
    {
        //
    }
}

The dd($invoices) will give a BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::company()

dd($invoices) 将给出 BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::company()

Any further needed information can be provided!

任何需要的信息都可以提供!

回答by Mahmoud Zalt

Let's say we have table A and B and C where table A has many of B (OneToMany) and B has many of C (OneToMany) inorder to access the table C from table A you can use the Laravel shortcut (HasManyThrough) on the Table A and the problem is solved

假设我们有表 A、B 和 C,其中表 A 有很多 B(OneToMany),B 有很多 C(OneToMany)为了从表 A 访问表 C,您可以在表上使用 Laravel 快捷方式(HasManyThrough) A 问题就解决了

BUTIf you have table A and B and C where table A has many of B (OneToMany) and B has many of C (ManyToMany) you cannot use the laravel's (HasManyThrough) shortcut to access the table C from table A, {because of the pivot table in the middle between B and C} what you can do in this case is very simple:

但是如果你有表 A 和 B 和 C,其中表 A 有很多 B (OneToMany) 并且 B 有很多 C (ManyToMany) 你不能使用 Laravel 的 (HasManyThrough) 快捷方式从表 A 访问表 C,{因为B 和 C 中间的数据透视表} 在这种情况下你可以做的很简单:

In this example table A will be [courses], table B will be [chapters], and table C will be [videos] where every course has may chapters, while a chapter can belong to only one course. in the other hand every chapter has many videos while a video can belong to many chapters.

在此示例中,表 A 将是 [课程],表 B 将是 [章节],表 C 将是 [视频],其中每门课程都有可能的章节,而一个章节只能属于一个课程。另一方面,每个章节都有许多视频,而视频可以属于多个章节。

<?php namespace Moubarmij\Models;

use Eloquent;

class Video extends Eloquent{

   protected $table = 'videos';

    /*************************************************************
     * Query Scopes
     **************************************************************/

    public function scopePublished($query)
    {
        return $query->where('published', '=', '1');
    }

    public function scopeOrdered($query)
    {
        return $query->orderBy('order_number', 'ASC');
    }

    /*************************************************************
     * Relations
     **************************************************************/

    public function chapters()
    {
        return $this->belongsToMany('Moubarmij\Models\Chapter', 'chapters_videos');
    }


}


<?php namespace Moubarmij\Models;

use Eloquent;

class Chapter extends Eloquent{

   protected $table = 'chapters';

    /*************************************************************
     * Query Scopes
     **************************************************************/

    public function scopePublished($query)
    {
        return $query->where('published', '=', '1');
    }

    public function scopeOrdered($query)
    {
        return $query->orderBy('order_number', 'ASC');
    }

    public function scopeWithVideos($query)
    {
        return $query->with(['videos' => function($q)
        {
            $q->ordered();
        }]);
    }


    /*************************************************************
     * Relations
     **************************************************************/

    public function course()
    {
        return $this->belongsTo('Course');
    }

    public function videos()
    {
        return $this->belongsToMany('Moubarmij\Models\Video', 'chapters_videos');
    }

}


<?php namespace Moubarmij\Models;

use Eloquent;

class Course extends Eloquent{

   protected $table = 'courses';

    /*************************************************************
     * Query Scopes
     **************************************************************/

    public function scopeVisible($query)
    {
        return $query->where('visible', '=', '1');
    }

    public function scopeOrdered($query)
    {
        return $query->orderBy('order_number', 'ASC');
    }

    public function scopeWithChapters($query)
    {
        return $query->with(['chapters' => function($q)
        {
            $q->ordered();
        }]);
    }

    public function scopeWithChaptersAndVideos($query)
    {
        return $query->with(['chapters' => function($q)
        {
            $q->ordered()->withVideos();
        }]);
    }


    /*************************************************************
     * Relations
     **************************************************************/

    public function chapters()
    {
        return $this->hasMany('Moubarmij\Models\Chapter');
    }


}

回答by Peter

You can also do this in the Course class, so when you use ->with('chapters'), it automatically loads the videos too:

您也可以在 Course 类中执行此操作,因此当您使用 ->with('chapters') 时,它也会自动加载视频:

public function chapters()
{
    return $this->hasMany('Moubarmij\Models\Chapter')->with('videos');
}