Laravel view foreach with two properties

Related searches

i need help with my foreach loop in view

Controller.php

$expenses = Expense::groupBy('category_id')->selectRaw('sum(amount) as sum, category_id')->pluck('sum','category_id');

$categories = Category::all();

return view('dashboard.index', compact('expenses','categories'));

index.php

          <table class="table">
            <thead>
              <th>Category</th>
              <th>Expenses</th>
            </thead>
            <tbody>
              @foreach($categories as $category)
              <tr>
                <td>{{ $category->name }}</td>
                @foreach($expenses as $expense)   
              <td>{{ $expense }}</td>
              @endforeach
              </tr>
              @endforeach
            </tbody>
          </table>

I get the output like this.

enter image description here

It outputs the expense 2 times, is there a limiter for "foreach loop" or is there another way on doing this?

You need to add a condition for that $category->id is the same as category_id

 @foreach($expenses as $key => $expense)   
    @if($key == $category->id)
    <td>{{ $expense }}</td>
    @endif
@endforeach

The right way to do it is by using join or relationship.

$expenses = Expense::join('categories','categories.category_id','expenses.category_id')->groupBy('expenses.category_id')->selectRaw('sum(expenses.amount) as sum, expenses.category_id','categories.name')->get();

<table class="table">
    <thead>
      <th>Category</th>
      <th>Expenses</th>
    </thead>
    <tbody>

         @foreach($expenses as $expense)   
          <th>{{ $expense->name }}</th>
          <td>{{ $expense->sum }}</td>
         @endforeach
  </tbody>
</table>

Laravel foreach with multiple objects?, This is a very old question but I had to do something similar and couldn't find anything on the internet. I put the second @foreach inside the <td>� {tip} All view composers are resolved via the service container, so you may type-hint any dependencies you need within a composer's constructor. Attaching A Composer To Multiple Views. You may attach a view composer to multiple views at once by passing an array of views as the first argument to the composer method:

You should limit the results in controller, but here's how you can accomplish this in a blade. Not pretty

@foreach ($element['subs'] as $item)
    @if($loop->index < 10)
        // Your code
    @endif
@endforeach

Blade Templates - Laravel, Laravel blade has a foreach directive that we can use the same way as we $ loop variable exposes eight useful properties. $loop->depth Returns the depth or nesting level of the current loop; returns 2 if it is a loop within a� Laravel blade has a foreach directive that we can use the same way as we use the foreach loop in PHP. So, you can do something like this in our view file

You should compare category_id with category->id.

@foreach($categories as $category)
<tr>
    <td>{{ $category->name }}</td>
    @foreach($expenses as $expense)
        @if($expense->category_id == $category->id )   
            <td>{{ $expense->sum }}</td>
        @endif
    @endforeach
</tr>
@endforeach

Using the $loop variable in foreach loops in Laravel, As the output shows, the foreach iterated through all of the visible properties that could be accessed. To take Example #2 Object Iteration implementing Iterator. The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything!

Object Iteration - Manual, home.blade.php Trying to get property of non-object (View: C:\xampp. <div class="row"> @foreach($services->split($services->count()/2)� By default, views are compiled on demand. When a request is executed that renders a view, Laravel will determine if a compiled version of the view exists. If the file exists, Laravel will then determine if the uncompiled view has been modified more recently than the compiled view. If the compiled view either does not exist, or the uncompiled

How to create two columns with @foreach?, If the child view requires these variables, you should use @foreach and @include instead. Stacks Blade allows you to push to named stacks which can be rendered somewhere else in another view or layout.

Become a Laravel Partner. Laravel Partners are elite shops providing top-notch Laravel development and consulting. Each of our partners can help you craft a beautiful, well-architected project.

Comments
  • try to add relation between expenses and categories. Then you can call expenses with categories so it will be easy to run only one loop. From laravel docs: laravel.com/docs/6.x/eloquent-relationships
  • I'm new to laravel, I will also try this. Thank you.
  • Thank you. I will try this later when i get home.
  • By saying not pretty, do you mean this is a bad practice? Thank you.
  • @JamesMacxy Its best practice to use Controller like a controller and use Views as a views, then we can call it MVC structure.
  • @JamesMacxy don't forget to validate my answer in case your problem is solved by it
  • how can i validate it?, your answer is working but as you said not pretty, i did not pursue it and tried to use other way and used dilip code also works for me.
  • Thank you. I will try this later when i get home.