gpt4 book ai didi

php - 如何精简 Laravel ORM

转载 作者:可可西里 更新时间:2023-10-31 22:40:37 26 4
gpt4 key购买 nike

不好意思,这个问题太蠢了..

我使用Controller,有很多ORM,我想精简这段代码。

我的 Controller

public function news(Request $request)
{
$history = Recruitments_status::where('recruitments_status.status',1)->get();
$history_a = Recruitments_status::where('recruitments_status.status',2)->get();
$history_b = Recruitments_status::where('recruitments_status.status',3)->get();
$history_c = Recruitments_status::where('recruitments_status.status',4)->get();
$history_d = Recruitments_status::where('recruitments_status.status',5)->get();
$history_e = Recruitments_status::where('recruitments_status.status',6)->get();

return view('pl_sidebar/news',[
'history' => $history,
'history_a' => $history_a,
'history_b' => $history_b,
'history_c' => $history_c,
'history_d' => $history_d,
'history_e' => $history_e
]);
}

如何精简这个ORM代码?

最佳答案

您的代码将生成多个查询,这是不好的做法。你可以获得一个集合:

public function news(Request $request)
{
return view('pl_sidebar/news', [
'history' => Recruitments_status::whereIn('status', [1, 2, 3, 4, 5, 6])
]);
}

然后在 Blade 模板中使用这个集合:

@foreach ($history->where('status', 1) as $single)
{{ $single->status }}
{{ $single->id }}
@endforeach

关于php - 如何精简 Laravel ORM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40484020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com