0 php oop laravel eloquent laravel-5
我正在构建使用递归 url 构造的 Laravel 应用程序。我想知道是否可以访问模型中 hasone 相关模型的数据,以将构造的 url 直接返回到视图,而无需与控制器交互。\
public function link(){
var_dump($this->category());
$url = ['news'];
$url[] = $this->category()->url;
$url[] = $this->url;
return implode('/',$url);
}
Run Code Online (Sandbox Code Playgroud)
像这样的简单代码示例返回这个
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)
Run Code Online (Sandbox Code Playgroud)
那么有没有什么好的方法可以通过仅使用雄辩的模型来解决它,或者只能通过使用控制器等来解决?
您不应该$this->category()作为函数调用。该函数调用关系,而不是相关模型。
要获取相关模型,请删除括号。
$this->category->url
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1321 次 |
| 最近记录: |