简体   繁体   English

Laravel模型关系

[英]Laravel Model relations

I cannot connect to model function in laravel 5.6 我无法连接到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">

here I call $item file_id to find the image 在这里我调用$ item file_id来查找图像

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

that I have in model to connect to other model to use this function 我在模型中可以连接到其他模型以使用此功能

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

but i get error like that enter image description here 但我收到这样的错误, 在此处输入图片描述

add with('photo') in the model to load the relation. 在模型中添加with('photo')以加载关系。 then check if $item->photo is not null 然后检查$item->photo是否不为空

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

then in the view 然后在视图中

<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