我正在构建使用递归 url 构造的 Laravel 应用程序。我想知道是否可以访问模型中与 hasone 相关的模型的数据,以将构造的 url 直接返回到视图,而无需控制器交互。\
public function link(){
var_dump($this->category());
$url = ['news'];
$url[] = $this->category()->url;
$url[] = $this->url;
return implode('/',$url);
}
像这样的简单代码示例返回这个
Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$url (View: /???/resources/views/common/news/full_preview.blade.php) (View: /???/resources/views/common/news/full_preview.blade.php) (View: /???/resources/views/common/news/full_preview.blade.php)
那么有没有什么好的方法可以通过使用雄辩的模型来解决它,或者只能通过使用控制器等来解决?
你不应该$this->category()
作为一个函数调用。该函数调用关系,而不是相关模型。
要获取相关模型,请删除括号。
$this->category->url
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句