Laravele başlayalı henüz 1 hafta oldu ve bugün karşılaştığım sorun bezdirdi. Hiçbir hata vermiyor ve viewe istediğim verileri çekmiyor. Amacım siparisdetay view'imde sipariş tablosunda bulunan ürünleri sipariş id'sine göre sıralamak. Fakat bir türlü gösteremedim.
Sepet modelimde bu fonksiyon ile SepetUrun modeline yönlendirip oradan da Urun modeline yönlendirmek istiyorum.
Sepet modeli;
public function sepeturun(){
return $this->hasMany('App\Models\SepetUrun');
}
SepetUrun modeli;
public function urun(){
return $this->belongsTo('App\Models\Urun');
}
Sipariş controllerinda detay actionu bu şekilde;
public function detay($id)
{
$siparis = Siparis::with('sepet.sepeturun.urun')->where('siparis.id', $id)->firstOrFail();
return view('siparisdetay', compact('siparis'));
}
siparisdetay view'im de bu şekilde;
@foreach($siparis->sepet->sepeturun as $sepetUrun)
<tr>
<td style="width: 120px">
<img src="http://via.placeholder.com/120x100">
</td>
<td>{{ $sepetUrun->urun->urunAdi }}</td>
<td>{{ $sepetUrun->fiyat }}</td>
<td>{{ $sepetUrun->adet }}</td>
<td>{{ $sepetUrun->adet * $sepetUrun->adet }}</td>
<td>{{ $sepetUrun->durum }}</td>
</tr>
@endforeach
Sipariş detaylarına baktığımda herhangi bir hata yok fakat veriler gelmiyor. dd($siparis) şeklinde kontrol ettiğimde şu şekil bir yapıyla karşılaşıyorum;
Siparis {#270 ▼
#table: "siparis"
#fillable: array:9 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:13 [▶]
#original: array:13 [▶]
#changes: []
#casts: []
#dates: array:1 [▶]
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"sepet" => Sepet {#284 ▼
#table: "sepet"
#guarded: []
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:5 [▶]
#original: array:5 [▶]
#changes: []
#casts: []
#dates: array:1 [▶]
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"sepeturun" => Collection {#283 ▼
#items: []
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#forceDeleting: false
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
#forceDeleting: false
}
Sorun nedir sıkıntı nerede yardımcı olabilecek varsa sevinirim.