Laravel渴望工作的客户
我真的很努力地努力与Laravel(Laravel 9)一起使用,我有一个页面,该页面正在创建一个n+1个盒子,告诉我我需要渴望加载客户端。我要做的是要获得属于用户的所有开放作业,并显示每个作业的客户标题(加上其他数据),现在我的工作类中有客户(雄伟的模型):
public function client()
{
return $this->hasOne( Client::class, 'id', 'client_id' );
}
我也有关系用户类可以使工作属于用户:
public function jobs()
{
return $this->hasManyThrough(
Job::class, // TargetTable
JobUser::class, // Intermediate table
'user_id', // Foreign key on intermediate table for current table
'number', // Foreign key on target table to intermediate table
'id', // Local key on current table used by intermediate table
'job_number' // Local key on intermediate table to target table
);
}
因此,从相关的控制器中,我得到了所有工作的收集:
public function getPlannerData()
{
$data = array();
$myJobs = = auth()->user()->jobs()->where( 'is_active', 1 )->get();
$data['jobs'] = $myJobs;
return $data;
}
这是将其放入数组中并传递给视图
/**
* Display listing of the resource
*
* @param Request $request
* @return \Illuminate\Contracts\View\View
*/
public function index( Request $request )
{
return View::make( 'pages.planner.index' )
->with( 'data', Planner::getplannerData() );
}
,最后在我视图中的组件中,我有以下内容在所有这些工作中循环浏览工作:
<h1 class="text-xl text-gray-100">Client: {{ $item->job->client->title }}</h1>
因此,我尝试添加('client')(以及('app \ models \ client''))到所有这些中,但我仍然收到消息(或者我没有得到数据)。
那么,我如何急切地为此集合中的工作加载客户端,以免收到消息?
该代码确实没有问题,因为它确实可以使用,并列出了客户标题的所有作业,但是如果我可以优化并删除n+1消息,那将非常有用!
我在其他页面上也有n+1个问题,因此我需要的是对它应该如何工作以及如何解决这些问题的全面了解。
最后,我确实有一些页面解决了急切的加载问题,但是这些与文档中所说的更加内联( https://laravel.com/docs/9.x/eloquent-relationships.ungers#eager-loading )$ jobs = jobs = ob job = job :: with('client' ') - &gt; where('is_Active',1) - &gt; get();
,但我希望获得属于用户而不是所有工作的工作(作业的用户都在乔布斯表中) 。
谁能帮助删除雾气并阐明应该如何做到这一点?
谢谢
***编辑***
感谢Tim Lewis的建议,我已经更新了问题。上面的线实际上是错误的行!应该是&lt; H1 class =“ text-xl text-gray-100”&gt; client:{{{$ item-&gt; job-&gt; client-&gt; client-&gt; title}}&lt;/h1&lt;/h1&gt; 我已经相应地更新了问题。我尝试在Job()之后尝试使用('client')添加 - &gt;,但我仍然收到n+1个消息。我错过了很多代码,因为在解决此问题时,大多数情况都没有被使用,在该应用程序的这一方面有很多代码,但是如果它会有所帮助,我会分享所有这些代码。
$ item是@foreach($ data ['jobs']的结果我仍然有相同的n+1问题,所以毕竟这可能不是组成部分(但我想不到我在此页面上使用的作业标题的其他位置 - 它列出了用客户名称的作业,没有什么else(禁止作业记录中的职位描述和状态标志)。
I'm really struggling with how eager loading works with Laravel (Laravel 9), I have a page which is creating an N+1 box telling me I need to eager load clients. What I'm trying to do is get all open jobs belonging to a user and to show the clients title for each job (plus other data) Now i have client in my Job class (eloquent model):
public function client()
{
return $this->hasOne( Client::class, 'id', 'client_id' );
}
I have also a relationship in my User class to get jobs belonging to a user:
public function jobs()
{
return $this->hasManyThrough(
Job::class, // TargetTable
JobUser::class, // Intermediate table
'user_id', // Foreign key on intermediate table for current table
'number', // Foreign key on target table to intermediate table
'id', // Local key on current table used by intermediate table
'job_number' // Local key on intermediate table to target table
);
}
So from the relevant controller I get a collection of all my jobs with this:
public function getPlannerData()
{
$data = array();
$myJobs = = auth()->user()->jobs()->where( 'is_active', 1 )->get();
$data['jobs'] = $myJobs;
return $data;
}
this is put into an array and passed to the view
/**
* Display listing of the resource
*
* @param Request $request
* @return \Illuminate\Contracts\View\View
*/
public function index( Request $request )
{
return View::make( 'pages.planner.index' )
->with( 'data', Planner::getplannerData() );
}
and finally in a component in my view I have the following while looping over the jobs:
<h1 class="text-xl text-gray-100">Client: {{ $item->job->client->title }}</h1>
So i have tried adding with('client') (and also with('App\Models\Client')) to in all of these but i still get the message (or i get no data).
So how do i eager load clients for the jobs in this collection so I do not get the message?
There is no issue with the code in that it does work and lists all the jobs with the clients titles but If I can optimise it and remove the N+1 message then that would be very useful!
I also have the N+1 issue on other pages so what I need is a thorough understanding on how this should work and how to resolve these issues.
Lastly I do have some pages where I have resolved the eager loading issues but these are more inline with what it says in the docs (https://laravel.com/docs/9.x/eloquent-relationships#eager-loading) $jobs = Job::with('client')->where('is_active', 1)->get();
but I wish to get jobs belonging to a user and not all jobs (the users of the job are in a JobUsers table).
Can anyone help remove the mist and shed some light on how this should be done?
thanks
*** EDIT ***
thanks to Tim Lewis's suggestions, I've updated the question. The line above is actually the wrong line! it should be <h1 class="text-xl text-gray-100">Client: {{ $item->job->client->title }}</h1>
I've updated the question accordingly. I have tried adding ->with('client') after job() but i still get the N+1 message. I have missed a lot of the code as mostly it is not being used while i get this resolved, there is a lot of code in this aspect of the app but I will share it all if it would help!.
The $item is a result of @foreach($data['jobs'] as $item
but maybe calling it job would be clearer with this. I have however removed the call to this in this component and I still have the same N+1 issue so it probably isn't the component after all (but I can't think where else i would be using a job title on this page - it lists the jobs with the client names and nothing much else (bar the job description and status flags which are in the Job record).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论