错误消息
方法衣柜图像不存在。 (查看:C:\ laragon \ www \ wasamar \ resources \ views \ main_app \ admin \ wardrobe.blade.php)
我有这个问题,当我想打电话给我的衣柜形象类来展示衣柜的装扮图像。
WARDROBE MODEL
<?php
namespace App\Wasamar;
use Illuminate\Database\Eloquent\Model;
class Wardrobe extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'wardrobes';
public function wardrobeimage(){
return $this->hasMany('App\Wasamar\Wardrobeimage','wardrobe_id');
}
}
WARDROBE IMAGE CLASS
<?php
namespace App\Wasamar;
use Illuminate\Database\Eloquent\Model;
class Wardrobeimage extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'wardrobeimages';
function wardrobe(){
return $this->belongsTo('App\Wasamar\Wardrobe','id');
}
}
所以当我这样做时它会提示错误
<?php
$wardrobeAttires = Wardrobe::where('user_id',$user->id)->get();
?>
$wardrobeAttires->wardrobeimage()->where('wardrobe_attire_view',0)->get();
答案 0 :(得分:0)
关系设置具有误导性。衣柜里有很多衣柜图片。命名约定应该是wardrobeimages()。如果wardrobe_id
是外键,则关系应为:
衣柜型号:
public function wardrobeimages(){
return $this->hasMany('App\Wasamar\Wardrobeimage','wardrobe_id');
}
<强> Wardrobeimage:强>
function wardrobe(){
return $this->belongsTo('App\Wasamar\Wardrobe','wardrobe_id');
}
在控制器中:
$wardrobeAttires = Wardrobe::with(['wardrobeimage'=>function($query)
{
$query->where('wardrobe_attire_view',0);
}])
->where('user_id',$user->id)->get();
这给出了衣柜的集合:
foreach($wardrobeAttires as $wardrobeAttire)
{
foreach($wardrobeAttire->wardrobeimages as $wardrobeimage)
{
print_r($wardrobeimage);
}
}
答案 1 :(得分:0)
我想通了,但感谢大家的支持。
@foreach( $wardrobeAttires as $wardrobeAttire)
<div class="row">
<div class="small-4 columns">
<div class="image-wrapper overlay-slide-in-left">
@if( count($wardrobeAttire->wardrobeimages()->where('wardrobe_attire_view',0)->first()) === 0 )
<img src="{{ domain_name() . '/f-assets/img/' . 'wardrobe_attire_default_image.png' }}" alt="Attire image" />
@else
@if( count($wardrobeAttire->wardrobeimages()->where('wardrobe_attire_view',0)->get()) >= 1 )
@foreach($wardrobeAttire->wardrobeimages()->where('wardrobe_attire_view',0)->get() as $wardrobeAttireImage)
<img src="{{ domain_name().'/ws-uploads/wardrobe-attire-image/thumbnail/'.$wardrobeAttireImage->new_name }}" alt="Attire image" />
@endforeach
@endif
@endif
<div class="image-overlay-content">
<h2>.overlay-slide-in-left</h2>
<p class="price">$2319.99</p>
<a href="#" class="button">Buy Me</a>
</div>
</div>
</div>
</div>
@endforeach
而不是$ wardrobeAttires应该是$ wardrobe在@foreach循环内部,这就是bug。