Merhaba arkadaşlar ürün resimlerini
resimler tablosunda tutuyorum, ürüne ait resimleri silmek istediğimde tümünü seç diyerek tüm resimleri siliyorum ,sonrasında ise hata alıyorum , sebebi ise modelim içindeki getKucukResimAttribute() , ve resim() methotlarımın çekeceği bir veri kalmaması, yani çekilecek resim olmadığı için döngü tamamlanamıyor ve hata alıyorum , sorum şu modelimdeki resim() methodundaki
MorphOne ile çekilecek bir veri kalmadığı zaman kucuk_resim =resim_yok.jpg veya boş değer alarak dönsün istiyorum bunu nasıl yapabilirim yardımcı olabilir misiniz. selamlar
tüm resimleri sildiğimde relationship MorphOne çalışmıyor ve şu hatayı alıyorum
ErrorException in Product.php line 26:
Trying to get property of non-object (View: C:\xampp\htdocs\L\konak2\resources\views\front\listele.blade.php)
modelimdeki 26.satırda şu var $resim= asset("uploads/urunler/thumb_".$this->resim()->first()->name);
Ürünleri return ile yazdırdığımda ise eğer ürüne ait resim var ise aşağıdaki gibi bir çıktı alıyorum burada sıkıntı yok .
ProductController : return $urunler=Product::all();
Normali
[
{
id: 1,
kat_id: 1,
name: "Kahvaltı Tabağı",
fiyat: "14.00",
adet: 1,
detay: "Kahvaltı Çeşitleri",
aciklama: "asdasd",
gununspesiali: null,
bizehas: null,
tavsiye: null,
created_at: "2017-10-17 08:24:21",
updated_at: "2017-10-17 08:24:21",
kucuk_resim: "<img src="http://127.0.0.1:8000/uploads/urunler/thumb_1508228661.jpg" alt="" class="img img-responsive">",
resim: "http://127.0.0.1:8000/uploads/urunler/thumb_1508228661.jpg"
}
]
Olmasını İstediğim aşağıda
[
{
id: 1,
kat_id: 1,
name: "Kahvaltı Tabağı",
fiyat: "14.00",
adet: 1,
detay: "Kahvaltı Çeşitleri",
aciklama: "asdasd",
gununspesiali: null,
bizehas: null,
tavsiye: null,
created_at: "2017-10-17 08:24:21",
updated_at: "2017-10-17 08:24:21",
kucuk_resim: null,
resim: null
}
]
Product Model.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $table="products";
protected $appends=["kucuk_resim","resim"]; //
protected $fillable = [
'kat_id','name','fiyat','adet','detay','aciklama'
];
public function resim(){
return $this->morphOne("App\Resim","imageable");
}
public function getKucukResimAttribute(){
$resim= asset("uploads/urunler/thumb_".$this->resim()->first()->name);
return '<img src="'.$resim.'" alt="" class="img img-responsive">';
}
public function getResimAttribute(){
$resim= asset("uploads/urunler/thumb_".$this->resim()->first()->name);
return $resim;
}
}