Laravel addSelect: Laravel 添加选择

huangapple go评论68阅读模式
英文:

Laravel addSelect

问题

我有一个查询,并尝试向其添加 addSelect(),但不起作用,我不知道为什么,根据文档似乎是正确的。

查询:

return Project::query()
    ->with('client')
    ->with('status')
    ->with('tasks')
    ->with('tasks.user.userAvatar')
    ->with('tasks.user')
    ->addSelect([
        'number_of_tasks' => Task::where('assigned_id', auth()->user()->id)->whereNull('finished_at')->count()
    ])

我得到这个错误:

列未找到:在'字段列表'中未知列 '4'

如果我将其输出为原始的 SQL 查询:

"select `4` from `projects`"

我尝试添加 select() 来选择 Project 中的所有内容,但什么都不起作用,我做错了什么?

英文:

I have a query, and I'm trying to add a addSelect() to it, but it won't work, and I don't know why, according to the docs it seems correct

The query:

return Project::query()
    ->with('client')
    ->with('status')
    ->with('tasks')
    ->with('tasks.user.userAvatar')
    ->with('tasks.user')
    ->addSelect([
        'number_of_tasks' => Task::where('assigned_id', auth()->user()->id)->whereNull('finished_at')->count()
    ])

I got this error:

Column not found: 1054 Unknown column '4' in 'field list'

If I output it as a raw sql query:

"select `4` from `projects`"

I am trying to add select() to it, to select everything from Project, but nothing works, what am I doing wrong?

答案1

得分: 1

你这样做是错误的。

这个:

Task::where('assigned_id', auth()->user()->id)->whereNull('finished_at')->count()

会得到一个整数。在这种情况下,我认为是 4,这会导致错误。

我认为你需要让 addSelect 参数成为一个 array<string, Builder>。尝试这样做:

->addSelect([
    'number_of_tasks' => Task::query()
        ->selectRaw('count(*)')
        ->where('assigned_id', auth()->user()->id)
        ->whereNull('finished_at');
])

另外,你可以在同一个 with() 调用中通过传递一个数组来预先加载多个关联。

return Project::query()
    ->with([
        'client',
        'status',
        'tasks',
        'tasks.user',
        'tasks.user.userAvatar',
    ])
    ->addSelect([
        'number_of_tasks' => Task::query()
            ->selectRaw('count(*)')
            ->where('assigned_id', auth()->user()->id)
            ->whereNull('finished_at'),
    ]);
英文:

You're going about it the wrong way.

This:

Task::where(&#39;assigned_id&#39;, auth()-&gt;user()-&gt;id)-&gt;whereNull(&#39;finished_at&#39;)-&gt;count()

Will result in an int. In this case, I think it's 4, which gives you the error.

I think you need to have the addSelect parameter be an array&lt;string, Builder&gt;. Try this:

-&gt;addSelect([
    &#39;number_of_tasks&#39; =&gt; Task::query()
        -&gt;selectRaw(&#39;count(*)&#39;)
        -&gt;where(&#39;assigned_id&#39;, auth()-&gt;user()-&gt;id)
        -&gt;whereNull(&#39;finished_at&#39;);
])

Also, you can eager load multiple relationships within the same with() call by passing in an array.

return Project::query()
    -&gt;with([
        &#39;client&#39;,
        &#39;status&#39;,
        &#39;tasks&#39;,
        &#39;tasks.user&#39;,
        &#39;tasks.user.userAvatar&#39;,
    ])
    -&gt;addSelect([
        &#39;number_of_tasks&#39; =&gt; Task::query()
            -&gt;selectRaw(&#39;count(*)&#39;)
            -&gt;where(&#39;assigned_id&#39;, auth()-&gt;user()-&gt;id)
            -&gt;whereNull(&#39;finished_at&#39;),
    ]);

huangapple
  • 本文由 发表于 2023年7月3日 16:36:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定