Well don't! Let your controller fetch all the data and return that to your view. In general your view should only display things and not do queries.
Can you show your current code so we can show an improvement on that?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
First i get id from outer loop then, through id Fetch Records Using Query Builder inside inner loop in view.. Does any body know about this..
Well don't! Let your controller fetch all the data and return that to your view. In general your view should only display things and not do queries.
Can you show your current code so we can show an improvement on that?
Actually, i want subcategories inside foreach categories loop using category->id based
I get Categories data using CategoriesServiceProvider class as i need this in several view pages.
public function boot() { view()->composer('partials.header',function($view){ $headerCategories=\DB::table('categories')->where('status','active')->get(); $view->headerCategories=$headerCategories; }); }
in view i used
@foreach($headerCategories as $categories) {{ $cid=$categories->c_id }}
how to get sub categories records here inside foreach loop?
I am new in laravel, I am unable to find answer help me here to get answer
is it possible to call route and return subcategories records inside foreach loop in view?
try this. if I understand you correctly this is what you need.
@foreach($headerCategories as $categories)
@foreach($categories->c_id as $subcategory)
//render here
{{subcategory->title}}
@endforeach
@endforech
if it's not please elaborate your question
yes, exactly you are right, but how i will get subcategories here busing query builder
Some one answer please, if you know.
Did you create some models for your tables ?
If not, read that : https://laravel.com/docs/5.6/eloquent
Did you write the relationship between subcategories and categories in the model ? If not, read that : https://laravel.com/docs/5.6/eloquent-relationships#one-to-many
If everything up is done, you can read that and apply it : https://laravel.com/docs/5.6/eloquent-relationships#eager-loading
In fact, it seems the same question than in your other thread : https://laracasts.com/discuss/channels/laravel/all-records-of-menu-with-sub-menu-using-query-builder
So same answer => Eloquent
There are times when a report or something uses sql in a loop. As example: See last reply here: https://laracasts.com/discuss/channels/laravel/complex-query-for-a-report
But I'd only do it in view if the inner foreach had a huge result. In which case you need double pagination.
What if inner foreach had 125,000 (just example) results such as a large Fedex accounts receivable report.
For a large year end report it is much better to dump data to a local machine then:
That's just the way I did things, somehow it worked.
Of course I know Fedex doesn't use Access for this, was an example. But the medium sized trucking company I worked for it worked great. And it was for monthly A/R, A/P reports.
Operations manager wanted printed out reports.
Thank You for answer, i must read eloquent.
Please or to participate in this conversation.