Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

elo's avatar
Level 3

Property [date] does not exist on this collection instance

I'm trying to display passed date from my controller in a blade view but getting the error above. Here's the code I'm working with

Product

{
    protected $guarded = ['id'];

    public function prices()
    {
        return $this->hasMany(Price::class, 'product_id');
    }
}```

Price
```class Price extends Model
{
    protected $guarded = ['id'];

    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id');
    }
}```

Controller
```public function index()
{
    $products = Product::with('prices')->orderBy('id')->get();

    $prices = Price::all()->groupBy('date');

    return view('home', compact('products', 'prices'));
}```

View
```@foreach($prices as $price)
    <div class="panel panel-default">
        <div class="panel-heading">
            {{ $price->date }}
        </div>

        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Price</th>
                    <th>Difference</th>
                    <th>Percentage</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                @foreach($price->product as $product)    
                    <td>{{ $product->name }}</td>
                @endforeach
                    <td>{{ $price->cost }}</td>
                    <td>0</td>
                    <td>0</td>
                </tr>
            </tbody>
        </table>
    </div>
@endforeach```

Tried commenting out ```{{ $price->date }}``` in my view and still got same error but this time it was the product property(keep in mind that products and prices tables have data in them).

Dumped $prices and i have the collection below
```Collection {#213 ▼
  #items: array:3 [▼
    "2017-08-04" => Collection {#207 ▼
      #items: array:3 [▼
        0 => Price {#235 ▶}
        1 => Price {#236 ▶}
        2 => Price {#237 ▶}
      ]
    }
    "2017-08-07" => Collection {#206 ▶}
    "2017-08-08" => Collection {#214 ▶}
  ]
}```

What am I doing wrong?
0 likes
8 replies
rumm.an's avatar
@foreach($prices as $priceGroup)
    @foreach($priceGroup as $price)
    <div class="panel panel-default">
        <div class="panel-heading">
            {{ $price->date }}
        </div>

        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Price</th>
                    <th>Difference</th>
                    <th>Percentage</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                @foreach($price->product as $product)    
                    <td>{{ $product->name }}</td>
                @endforeach
                    <td>{{ $price->cost }}</td>
                    <td>0</td>
                    <td>0</td>
                </tr>
            </tbody>
        </table>
    </div>
    @endforeach 
@endforeach

This will work. But Why are you grouping them? I see there is no point You are not taking any advantage of grouping.

elo's avatar
Level 3

@rumm.an Tried doing it like in your code but got the error "Trying to get property of non-object" so i commented out the {{$product->name}} and the rest of the code worked. So that's one issue less, any thoughts on how to resolve it? I also noticed that my result isn't grouped as you pointed out. Reason I wanted to group is so that for each date, a table is displayed with the product name column and the cost column. so if i have 3 different dates, that will be 3 different tables. With my current code, I'm displaying 9 tables which isn't what i want. Any thoughts on the best way to achieve desired result?

elo's avatar
Level 3

@rumm.an Ok I've got the product name working now, removed the foreach loop and displayed it using

<td>{{ $price->product->name }}</td>

How do I write my query so that each day has it's own table as i explained earlier?

Snapey's avatar

it might help if you consider the effect of grouping

You don't have a collection of prices, you have a collection of dates, each date can have many prices so you need to work from the outside date element first

@foreach($dates as $date)
    // start a new table
    @foreach($prices as $price)
        //show each table row
    @endforeach 
    //close the table
@endforeach
public function index()
{

    $dates = Price::with('product')->all()->groupBy('date');

    return view('home', compact('dates'));
}
elo's avatar
Level 3

@Snapey Tried it out and got a table for each date for each product. Similar to the previous code. Had little sleep trying to figure this out, still sweating it.

rumm.an's avatar
rumm.an
Best Answer
Level 17
@foreach($prices as $date => $priceGroup)
    <div class="panel panel-default">
        <div class="panel-heading">
            {{ $date }}
        </div>

        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Price</th>
                    <th>Difference</th>
                    <th>Percentage</th>
                </tr>
            </thead>
            <tbody>
              @foreach($priceGroup as $price)    
                <tr>
                    <td>{{ $price->product->name }}</td>
                    <td>{{ $price->product->cost }}</td>
                    <td>0</td>
                    <td>0</td>
                </tr>
              @endforeach
            </tbody>
        </table>
    </div>
@endforeach

Hope this helps.

1 like
elo's avatar
Level 3

@rumm.an problem solved, thanks to your solution. If you have the time, please drop a note for those of us learning so we understand better what we were doing wrong. Cheers

Please or to participate in this conversation.