Laravel 5有很多 [英] Laravel 5 hasManyThrough
问题描述
我有3个表:company
<-> users
<-> invoice
.
I have 3 tables: company
<-> users
<-> invoice
.
公司hasMany
用户.
用户belongsTo
公司和用户hasMany
发票.
发票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:
发票hasManyThrough
用户,公司(因此通过用户获取公司)
An invoice hasManyThrough
users, company (so gets the company through user)
现在它无法正常工作.
型号:
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');
}
}
发票控制器:
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)
{
//
}
}
dd($ invoices)将给出一个BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::company()
The dd($invoices) will give a BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::company()
任何其他需要的信息都可以提供!
Any further needed information can be provided!
推荐答案
假设我们有表A,表B和表C 其中表A有许多B(OneToMany),而表B有许多C(OneToMany) 为了从表A访问表C,您可以在表A上使用Laravel快捷方式(HasManyThrough),问题已解决
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(ManyToMany) 您不能使用laravel的(HasManyThrough)快捷方式从表A访问表C,{因为数据透视表位于B和C之间的中间}在这种情况下,您可以执行以下操作:
BUT If 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为[视频] 每门课程可能包含几章,而一章只能属于一门课程.另一方面,每个章节都有很多视频,而一个视频可以属于多个章节.
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.
<?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');
}
}
这篇关于Laravel 5有很多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!