[silindi]
Şu şekilde fiyat sayısını alabilirsiniz.
$priceCount = $product->prices()->count();
index() yöntemi de şöyle olabilir:
$product=Product::with(['prices' => function($query) {
$query->where("created_at",">=", now()->subDays(30));
}])
->withCount('prices')
->find($id);
return view('admin.detail', compact('product'))
resources/views/admin/detail.blade.php içinde de:
# Price sayısı. Bunu Laravel withCount kullandığım için otomatik oluşturuyor
{{ $price->prices_count }}
@foreach($product->prices as $price)
{{ $price->price }}
# created_at ve updated_at otomatik Carbon objesine dönüştürülür
# protected $dates alanına siz de kendi tarihlerinizi ekleyebilirsiniz
{{ $price->created_at->format('M j, Y H:i')}}
@endforeach
Belli ki siz de diğer bazı arkadaşlar gibi hiç dokümanları incelemeden sağdan soldan bulduklarınızla, kendi denemelerinizle ilerliyorsunuz.
https://laravel.com/docs/7.x/eloquent
https://laravel.com/docs/7.x/eloquent-relationships#eager-loading
https://laravel.com/docs/7.x/eloquent-relationships#counting-related-models
https://laravel.com/docs/7.x/eloquent-mutators#date-mutators