在laravel的急切装载中,从多对多的关系中获取第一个或最新的第一个

Nit*_*mar 5 php laravel laravel-collection laravel-eloquent laravel-5.4

我正在构建一个小应用程序laravel 5.4,我有两个模型Contact,Companies我之间有很多很多关系,在我的Contact Model:

public function company()
{
    return $this
        ->belongsToMany('App\Company', 'company_contact','contact_id', 'company_id')->withTimestamps();
}
Run Code Online (Sandbox Code Playgroud)

现在在某个地方,我希望拥有现有公司,即我想拥有latest() first().或者orderBy,created_by desc并获得first()排.为此,我必须做这样的事情:

$contacts = Contact::where('name', 'LIKE', '%'. $request->search_input. '%')
    ->orWhere('email', 'LIKE', '%'. $request->search_input. '%')
    ->orWhereHas('company', function ($q) use($request) {
        $q->where('name', 'LIKE', '%'. $request->search_input. '%');
    })
    ->with('company')
    ->orderBy('created_at', 'desc')
    ->paginate(50);
foreach ($contacts as $contact)
{
    $contact->company = $contact->company()->withPivot('created_at')->orderBy('pivot_created_at', 'desc')->first();
}
Run Code Online (Sandbox Code Playgroud)

为了删除foreach我尝试在我的Contact模型中使用新关系:

public function currentCompany()
{
    return $this
        ->belongsToMany('App\Company', 'company_contact','contact_id', 'company_id')
        ->withTimestamps()
        ->orderBy('created_at', 'desc')
        ->first();
}
Run Code Online (Sandbox Code Playgroud)

但是在控制器中取出时:

$contacts = Contact::where('name', 'LIKE', '%'. $request->search_input. '%')
    ->orWhere('email', 'LIKE', '%'. $request->search_input. '%')
    ->orWhereHas('currentCompany', function ($q) use($request) {
        $q->where('name', 'LIKE', '%'. $request->search_input. '%');
    })
    ->with('CurrentCompany')
    ->orderBy('created_at', 'desc')
    ->paginate(50);
Run Code Online (Sandbox Code Playgroud)

但是它给我带来了错误,是否有任何eloquent方式或Collection方法可以删除它foreach.

Soh*_*415 1

使用first()内部封闭-

$contacts = Contact::where('name', 'LIKE', '%'. $request->search_input. '%')
->orWhere('email', 'LIKE', '%'. $request->search_input. '%')
->with(['company'=>function ($q) use($request) {
    $q->where('name', 'LIKE', '%'. $request->search_input. '%')->first();
}])
->orderBy('created_at', 'desc')
->paginate(50);
Run Code Online (Sandbox Code Playgroud)

或者像这样——

$contacts = Contact::where('name', 'LIKE', '%'. $request->search_input. '%')
->orWhere('email', 'LIKE', '%'. $request->search_input. '%')
->orWhereHas('company', function ($q) use($request) {
    $q->where('name', 'LIKE', '%'. $request->search_input. '%');
})
->with(['company'=>function($q){
       $q->first();  
}])
->orderBy('created_at', 'desc')
->paginate(50);
Run Code Online (Sandbox Code Playgroud)

这样你就不需要做任何additional foreach循环。