尝试获取属性' url'非对象(查看:d:\ xampp71 \ htdocs \ weblot-ikan \ resources \ view \ gizi \ gizi \ listproduct.blade.blade.php)
我在建立一对一关系的模型方面有问题。这是我的页面代码,
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您这样做时,
您必须进行防御性编码。如果我没有一个foto怎么办?您应该为foto做好准备,
以避免错误的最简单是与零结合操作员一起使用,而
当ikan没有foto时,您想做的任何事情都不会更改。
When you do this
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
change NO FOTO to whatever you want to do when Ikan does not have foto