Support the ongoing development of Laravel.io →
Database Views

Howdy all,

I'm currently building a dashboard for an order system Ive been building in L5(first project by the way).

What I can't wrap my brain around is if I should be passing all of the data collections to the view and then pulling out the info I need to display. Or if I should use Ajax calls to pull in the data after the page loads.

Maybe I'm doing something wrong from the start, but what I'm worried about is that the data collections will get large eventually...

My basic controller looks like this:

	public function index()
	{
  		$orders = Order::all();

		return view('admin.pages.index')->with('orders', $orders);
	}

pulling in all of the Orders with 1 query and then in the view I do things like this to get counts for different order statuses:

    {{ $orders->where('status', 2, false)->count() }}

    {{ $orders->where('status', 3, false)->count() }}

This seems fine for when I have a small number of orders, but down the line say I have 20,000 orders in the system. that seems like a huge array to be passing and parsing in the view.

With Ajax I could setup some routes that pull this specific data directly from the DB and not have to deal with the collections, but that would mean a lot more queries against the database every page load.

I guess my 3rd option would be to pass a custom $data array to the view with the data already parsed out, but that doesn't help pulling in all of the Orders and their data on every page load.

Any guidance would be appreciated

Last updated 2 years ago.
0

You can use pagination to fetch orders in small chunks.

Laravel's pagination feature is very simple to use. Basically, you use paginate() method on your model where you would use all() or get(), and Laravel handles the rest. You will also need to render pagination controls (links to prev/next page) in your view, which is a single line of code.

0

also this Trait will help you to send data to view , its doesn't related to your question but its really helpful to use in your application .

This issue in laravel has been resolved just by using this class in your controllers as a Trait .

http://laravel-tricks.com/tricks/easy-passing-data-to-views

0

Sign in to participate in this thread!

Eventy

Your banner here too?

seansch seansch Joined 5 Apr 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.