'Trying to get property 'Url' of non-object (View: D:\XAMPP71\htdocs\website-ikan\resources\views\gizi\listproduct.blade.php)

I have a problem with making model for one to one relationship. this is my code for the page

<div class="col-sm-8 container">
            <div class="row">
                @foreach ($ikan as $i)
                <div class="col-lg-4 col-md-6 col-sm-10 offset-md-0 offset-sm-1" onclick="location.href='/';">
                    <div class="card"> <div class="container-gambar" style="aspect-ratio: 3/2;"><img class="card-img-top w-100 h-100" style="object-fit: contain" src="{{$i->fotos->Url}}"></div>
                        <div class="card-body">
                            <h6 class="font-weight-bold pt-1">{{$i->nama_ikan}}</h6>
                            <div class="text-muted description">Space for small product description</div>
                                <div class="d-flex flex-column">
                                    <div class="h6 font-weight-bold">{{$i->hargas->harga}}</div>
                                </div>
                        </div>
                    </div>
                </div>
                @endforeach
            </div>
        </div>

This is my code for the models : <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\HargaIkan;

class Ikan extends Model
{
    use HasFactory;

    protected $table = "ikan";

    public function fotos(){
        return $this->hasOne('App\Models\FotoIkan', 'ikan_id');
    }

    public function hargas(){
        return $this->hasOne(HargaIkan::class, 'ikan_id');
    }
}

this is also my code for the other model : <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Ikan;

class HargaIkan extends Model
{
    use HasFactory;

    protected $table = "harga_ikan";

    public function ikans(){
        return $this->belongsTo(Ikan::class, 'ikan_id');
    }
}

and this is the another one : <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class FotoIkan extends Model
{
    use HasFactory;

    protected $table = "foto_ikan";

    public function ikans(){
        return $this->belongsTo('App\Models\Ikan', 'ikan_id')->withDefault([
            'harga' => 'Tidak diketahui',
        ]);
    }
}

and this is my controller :

public function listprodukgizi(){
        $ikan = Ikan::all();

        return view('gizi.listproduct', ['ikan'=>$ikan]);
    }

Do you have any solution for this? i checked my database and the name for the columns is right. I also check the result for the relation of each array and it doesnt have relation.



Solution 1:[1]

When you do this

{{$i->fotos->Url}}

you have to code defensively. What if one of $i does not have a foto? You should be prepared for foto to be null

easiest to avoid error is with the null coalesce operator

{{$i->fotos->Url ?? 'NO FOTO'}}

change NO FOTO to whatever you want to do when Ikan does not have foto

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Snapey