我正在使用laravel 5.5 / php 7.2 /
例如,我在模型中具有如下功能:
public function featuredTopSight($count=8)
{
return $this->hasMany(Sight::class)
->wherePublish(1)
->latest()
->take($count);
}
然后,在视图中:
$sight->featuredTopSight(8);
但是我得到了这个错误:
"htmlspecialchars() expects parameter 1 to be string, object given
但是有了$sight->featuredTopSight;
我正确的答案。但我无法传递参数。
在该函数中,您正在定义一种关系。
您不能使用参数调用关系。
首先,您需要声明该关系,然后是要检索结果的查询或方法。
您的方法应类似于:
public function sights()
{
return $this->hasMany(Sight::class);
}
public function getFeaturedTopSights($count = 8)
{
return $this->sights()->wherePublish(1)->latest()->take($count)->get();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句