在此代码中,如下所示:
$latestHerbsInformation = \App\ContentCategories::find('14')->contents->take(5);我想使用inRandomOrder()从内容中获取随机行,Contents和ContentCategories是belongsToMany RelationShips,代码如下:
$latestHerbsInformation = \App\ContentCategories::find('14')->contents->inRandomOrder()->take(5);别为我工作。
Contents型号:
class Contents extends Model
{
use Sluggable;
protected $table = 'contents';
protected $guarded = ['id'];
public function categories()
{
return $this->belongsToMany(ContentCategories::class);
}
}ContentCategories型号:
class ContentCategories extends Model
{
protected $table = 'contents_categories';
protected $guarded = ['id'];
public function contents()
{
return $this->belongsToMany(Contents::class);
}
}我得到了这个错误:
Method inRandomOrder does not exist. 我该如何解决这个问题?提前感谢
发布于 2018-01-19 15:52:13
正确的查询是:
\App\ContentCategories::find('14')->contents()->inRandomOrder()->take(5)->get();因为这将执行查询并返回一个集合:
\App\ContentCategories::find('14')->contentshttps://stackoverflow.com/questions/48336383
复制相似问题