Hello,
I working on a small price comparison site/script with Laravel as the frontend part of the backend. But having some problems with the relationships and showing them.
I can show the prices on the product page. But not the store names and could not figure out how I could do that.
Is this because there is a problem in my database design or am I just stupid and it's an easy fix?
Error: "Attempt to read property "shopname" on null"
Thanks for the help :)
My database design:

My migration tables:
Stores:
$table->id();
$table->string('shopname');
....
Product:
$table->id();
$table->string('productname');
$table->foreignId('category_id')->constrained('product_categories');
$table->foreignId('brand_id')->constrained('product_brands');
....
Price:
$table->id();
$table->decimal('priceproduct');
$table->foreignId('product_id')->constrained('products');
$table->foreignId('store_id')->constrained('stores');
....
My models:
Store:
public function prices(){
return $this->hasMany(Price::class);
}
Product:
public function product_categories(){
return $this->hasMany(ProductCategory::class);
}
public function product_brands(){
return $this->hasOne(ProductBrand::class);
}
public function prices(){
return $this->hasMany(Price::class)->with('stores');
}
Price:
public function products(){
return $this->belongsTo(Product::class);
}
public function stores(){
return $this->belongsTo(Store::class);
}
Product controller
public function show($id){
$product = Product::find($id);
return view('products.show', compact('product'));
}
Product view:
{{$product->productname}}
@foreach ($product->prices as $price)
{{ $price->stores->shopname }} - {{ $price->priceproduct }}
@endforeach