我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

Laravel - 使用查询功能会出错

I'm trying to take $max results. But sometimes the $max is NULL, so the query must returns all results.

I use this code:

$results = MyModel::whereIn('player', $players)->take(function($query) use ($max) {
    if (isset($max)) {
        $query->take($max);
    }
})->get();

But i get this error:

[ErrorException]                                       
  Object of class Closure could not be converted to int 

You may try this:

$query = MyModel::whereIn('player', $players);
if (!empty($max)) {
    $query->take($max);
}

$result = $query->get();

近期文章

  • 在while循环中组合两个SQL查询
  • 在php shell_execute中执行qpdf
  • 如何找到sql结果数组的差异
  • 为什么Symfony2存储库中有C文件?
  • 扩展错误消息变量
  • 强制下载标头不能按预期工作
  • 处理文件和Web服务器中的不同用户
  • 在简单的mvc中使用模型到控制器
  • PHP loadHTML错误解决方案会产生xPath查询问题
  • Mysql表示表不存在。 可能有什么不对?
  • 如何理解更新语句是否成功运行?
  • 如何获取可排序的portlet位置并在移动时更新它?
  • 使用fgetcsv()解析数据 - 期望参数为资源
  • woocommerce更新购物车功能刷新页面到错误的域
  • 无法使用Slim Framework读取“Authorization”HTTP标头
  • .htaccess链接到root
  • 通过AJAX将动态参数从JavaScript传递到PHP,以便在cURL调用中使用
  • 从数据库填充Datalist
  • MySQL自动递增时发出日期
  • laravel 5.1播种时未定义的表

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华