Laravel 5 hasManyThrough

Lia*_*iam 8 php laravel

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

一个公司的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');
    }
}
Run Code Online (Sandbox Code Playgroud)

发票控制器:

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)
    {
        //
    }
}
Run Code Online (Sandbox Code Playgroud)

dd($发票)会给出一个 BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::company()

可以提供任何进一步的信息!

Mah*_*alt 7

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

但是如果你有表A和B和C,其中表A有许多B(OneToMany)而B有很多C(ManyToMany)你不能使用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');
    }


}
Run Code Online (Sandbox Code Playgroud)
<?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');
    }

}
Run Code Online (Sandbox Code Playgroud)
<?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');
    }


}
Run Code Online (Sandbox Code Playgroud)