【问题标题】:Laravel 4.2 - can't get random rows from databaseLaravel 4.2 - 无法从数据库中获取随机行
【发布时间】:2014-11-29 16:25:39
【问题描述】:

有人可以解释一下为什么会这样

$profile = Profile::all()->random(8);

这不起作用

$profile = Profile::where('gender_id', '=', 1)->random(8);

我收到错误提示

调用未定义的方法 Illuminate\Database\Query\Builder::random()

【问题讨论】:

    标签: laravel-4 eloquent


    【解决方案1】:

    你应该使用:

    $profile = Profile::where('gender_id', '=', 1)->get()->random(8);
    

    因为random() 函数从集合中获取随机项。 Reference

    【讨论】:

    • 这就是答案。我放错地方了
    【解决方案2】:

    添加 ->get()

    $profile = Profile::where('gender_id', '=', 1)->get()->random(8);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 2021-07-02
      • 2014-04-02
      • 2017-04-26
      • 2016-01-15
      • 2017-07-06
      • 1970-01-01
      相关资源
      最近更新 更多