尝试获取属性' url'非对象(查看:d:\ xampp71 \ htdocs \ weblot-ikan \ resources \ view \ gizi \ gizi \ listproduct.blade.blade.php)

发布于 2025-01-21 18:31:03 字数 2722 浏览 0 评论 0原文

我在建立一对一关系的模型方面有问题。这是我的页面代码,

<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>

这是我的模型代码: &lt;?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');
    }
}

这也是我的其他模型的代码: &lt;?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');
    }
}

,这是另一个: &lt;?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',
        ]);
    }
}

,这是我的控制器:

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

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

您对此有任何解决方案吗?我检查了数据库,列的名称是正确的。我还检查了每个数组之间关系的结果,并且没有关系。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在看孤独的风景 2025-01-28 18:31:03

当您这样做时,

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

您必须进行防御性编码。如果我没有一个foto怎么办?您应该为foto做好准备,

以避免错误的最简单是与零结合操作员一起使用,而

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

当ikan没有foto时,您想做的任何事情都不会更改。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文