php - How to streamline Laravel ORM -
excuse me, stupid question..
i use controller, has many orm , want streamline code.
my contorller
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 ]); }
how streamline orm code?
your code generate multiple queries bad practice. can collection:
public function news(request $request) { return view('pl_sidebar/news', [ 'history' => recruitments_status::wherein('status', [1, 2, 3, 4, 5, 6]) ]); }
then in blade template use collection:
@foreach ($history->where('status', 1) $single) {{ $single->status }} {{ $single->id }} @endforeach
Comments
Post a Comment