【发布时间】:2019-04-04 14:29:57
【问题描述】:
我无法连接到 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);
}
但我得到这样的错误 enter image description here
【问题讨论】:
-
在照片模型中声明了 file_url() 吗?如果不是,它在哪里声明?
-
挂接调试器,找到返回错误的$item,验证文件id是否存在于代表Staff_files的表中。您可能需要对 FK 进行更好的参照完整性检查。
-
检查关系是否存在而不是file_id是否存在不是更好吗?即
$item->photo ? $item->photo->file_url() : "http://www.ecmsnews.com/wp-content/themes/nucleare-pro/images/no-image-box.png" -
@SteveNosse $item->photo->file_url() is $item->model->model
-
@Devon $item->file_id 有 [932]