繁体   English   中英

Laravel模型关系

[英]Laravel Model relations

我无法连接到laravel 5.6中的模型函数

     <img src="{{$item->file_id ? $item->photo->file_url() : 
    "http://www.ecmsnews.com/wp-content/themes/nucleare-pro/images/no-image-box.png"}}"  
     width="200" height="100">

在这里我调用$ item file_id来查找图像

public function photo(){
       return $this->belongsTo('App\Models\CRM_STAFF\Staff_files', 'file_id');
  }

我在模型中可以连接到其他模型以使用此功能

 public function file_url(){
        return ($this->_domain).($this->id);
}

但我收到这样的错误, 在此处输入图片描述

在模型中添加with('photo')以加载关系。 然后检查$item->photo是否不为空

$item = Item::with('photo')->where(...)->get();
return view('yourview', compact('item'));

然后在视图中

<img src="{{!is_null($item->photo) ? $item->photo->file_url() : "http://www.ecmsnews.com/wp-content/themes/nucleare-pro/images/no-image-box.png"}}" width="200" height="100">

谢谢所有想帮助我的人,我找到了答案。问题是我用Json_encode保存了所有文件,我只需要对其解码即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM