Laravel 5 hasManyThrough

Laravel 5 hasManyThrough

问题描述:

我有3个表格:company < - >users < - >invoiceLaravel 5 hasManyThrough

一家公司hasMany用户。

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

发票belongsTo用户。

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

发票hasManyThrough用户,公司(,就可以得到该公司通过用户)

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

型号:

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($发票)会给可以提供BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::company()

任何进一步需要的信息!

比方说,我们有表A和B和C 其中表A有许多B(一对多)和B有很多C(一对多) 的序访问从表A中的表C可以使用Laravel快捷方式(HasManyThrough)在桌子上,问题就解决了

如果你有表A和B和C 其中表A有许多B(一对多)和B有很多C(多对多)的 你不能使用laravel(HasManyThrough)快捷方式访问表A中的表C,{因为B和C之间中间的枢轴表}在这种情况下,您可以做的事情非常简单:

在这个例子中,表格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'); 
    } 


} 
+0

我是否也必须使用查询范围或者只是关系? – Liam

+0

@Synyster你不必使用范围,但他们是非常值得推荐的,因为如果没有它们,你会在每个使用它的地方重复这种代码的和平。然而,在本例中最重要的范围是,如果你想在不使用示波器的情况下得到结果,那么'scopeWithChaptersAndVideos'将不得不编写其内容,并用视频模型中的内容替换'withVideos'。 –

你也可以做到这一点的课程班,所以当你使用 - >使用( '章节'),它会自动加载视频太:

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

多年后,这是我的解决方案。更简单! – udog