【发布时间】:2014-11-29 16:25:39
【问题描述】:
有人可以解释一下为什么会这样
$profile = Profile::all()->random(8);
这不起作用
$profile = Profile::where('gender_id', '=', 1)->random(8);
我收到错误提示
调用未定义的方法 Illuminate\Database\Query\Builder::random()
【问题讨论】:
有人可以解释一下为什么会这样
$profile = Profile::all()->random(8);
这不起作用
$profile = Profile::where('gender_id', '=', 1)->random(8);
我收到错误提示
调用未定义的方法 Illuminate\Database\Query\Builder::random()
【问题讨论】:
你应该使用:
$profile = Profile::where('gender_id', '=', 1)->get()->random(8);
因为random() 函数从集合中获取随机项。 Reference
【讨论】:
添加 ->get()
$profile = Profile::where('gender_id', '=', 1)->get()->random(8);
【讨论】: