Home > Software design >  How to Get organized data from Database in Laravel
How to Get organized data from Database in Laravel

Time:01-30

I am self learner and building a Web app for inhouse Dairy Farm project and stuck at one point where i want to show below data in Chart.

  1. Total milk collected - Day wise - for Duration ( Say 30 Days )
  2. Total Milk Collected - Animal Wise - For Duration ( Say 30 Days )

Ref. Attached image which show the data i stored in data. Can help me how can i build query in Laravel to get desired data in expected format.

Thanks in Advance.... enter image description here

CodePudding user response:

I don't understand clearly the problem, but actually you just have to read documentation about relationship https://laravel.com/docs/8.x/eloquent-relationships.

You need to build a relational model that can help you through this problem. I see Animal data, and also "Storage" data, with a sort of category (Milk, cereals, whatever). So the "Storage" are link to the animals. With theses relations, you will receive collection, and then you can count how many results you will have with it.

Exemple :

$animals = Animal::all()

@foreach($animals as $animal)
  <tr>
     <td>{{ $animal->id }}</td>
     <td>{{ $animal->storages->count() }}</td>
  </tr>
@endforeach

i know that it dont match perfectly with your need, but i think that it's a "clue" :)

  •  Tags:  
  • Related