【问题标题】:Laravel: Check if MySQL query has resultLaravel:检查 MySQL 查询是否有结果
【发布时间】:2017-07-24 05:04:59
【问题描述】:

第一个 Laravel 项目。我有一个控制器功能,它检查是否有任何带有该条形码的记录。如果没有插入记录。如果是,则加一。

public function sellmode(Request $request){
    $barcode=$request->barcode;
    $test = DB::select('select count from sellmode where barcode = ?', [$barcode]);
    $row_cnt = mysqli_num_rows($test);
    if ($row_cnt == 0) {
        Debugbar::info('Új sor');
        DB::insert('insert into sellmode (barcode, count) VALUES (?, ?)', [$barcode, '1']); 
    } else {
        Debugbar::info('Meglévő frissítése');
        DB::update('update sellmode set count = count + 1 WHERE barcode = ?' [$barcode]);
    }
    return view(sell);

}

当我尝试它时,它有以下错误:

SellController.php 第 17 行中的 ErrorException:mysqli_num_rows() 期望参数 1 是 mysqli_result,给定数组

我做错了什么?

【问题讨论】:

  • 你不应该在这里使用mysqli_num_rows 或任何与mysqli 相关的函数。 Laravel 使用 PDO。虽然为了论证,如果您想查看返回了多少行,您可以只计算结果数组中的值的数量...$row_cnt = count($test);

标签: php mysql laravel laravel-5


【解决方案1】:

您不能只在 Laravel 查询构建器上使用 mysql_num_rows。 Laravel 查询生成器会返回一个collection,所以你可以使用isEmpty 函数来查看它是否有任何结果。

if ($test->isEmpty()) {
    Debugbar::info('Új sor');
    DB::insert('insert into sellmode (barcode, count) VALUES (?, ?)', [$barcode, '1']); 
} else {
    Debugbar::info('Meglévő frissítése');
    DB::update('update sellmode set count = count + 1 WHERE barcode = ?' [$barcode]);
}

如果您使用的是 5.3 之前的 Laravel 版本,查询构建器将返回一个数组。在这种情况下,您可以在这个数组上使用 php count 函数来知道返回了多少行

if (count($test) === 0) {
    Debugbar::info('Új sor');
    DB::insert('insert into sellmode (barcode, count) VALUES (?, ?)', [$barcode, '1']); 
} else {
    Debugbar::info('Meglévő frissítése');
    DB::update('update sellmode set count = count + 1 WHERE barcode = ?' [$barcode]);
}

【讨论】:

  • 谢谢,但我收到另一个错误:“SellController.php 第 17 行中的 FatalThrowableError:调用数组上的成员函数 isEmpty()”
  • 在这种情况下,您可能使用的是低于 5.3 的 Laravel 版本。我已经用这些版本的解决方案更新了我的答案。
  • 我以为我使用的是 5.3,但现在我运行了“php artisan --version”,输出为“Laravel Framework 5.4.13”。我也尝试了第二个命令,“新行”正在工作,但“更新”的输出是这个错误消息: SQLSTATE [42000]:语法错误或访问冲突:1064 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“o”附近使用正确的语法(SQL:o)
【解决方案2】:

创建一个模型并使用它来查询数据库是个好主意。也许是这样的(在我看来这更容易):

$sellMode = SellMode::where('barcode', $barcode)->get();
if($sellMode->isEmpty()){
   Debugbar::info('Új sor');
   $sellMode = SellMode::create(['barcode' => $barcode, 'count' => 1]);
}
else{
    Debugbar::info('Meglévő frissítése');
    $sellMode->increment('count');
}

【讨论】:

【解决方案3】:

当我使用test->isEmpty()count($test) == 0 时,我仍然不断收到此错误:

未定义的偏移量:0

所以我最终使用它来检查来自 DB::select: 的空返回值:

$test = DB::select('select count from sellmode where barcode = ?', [$barcode]);

if(collect($test)->first()) {
    // ...
} else {
    $response = new \stdClass();
    $response->error = true;
    $response->message = 'Sellmode not found';
    return response(json_encode($response))->header('Content-Type','application/json');                
}

【讨论】:

    【解决方案4】:

    三个优雅的 Laravel 收集方法是 isEmpty()、count() 和 first()。 Laravel 中的 vanillaPHP 不再需要使用 Count。

    $result = Model::all();
    // if your resultset has something
    if ($result->isEmpty()) // return false
    if ($result->count() > 0) // return false
    if ($result->first()) // return model object
    
    // if your resultset has no entries
    if ($result->isEmpty()) // return true
    if ($result->count() > 0) // return true
    if ($result->first()) // return null
    

    【讨论】:

      猜你喜欢
      • 2014-01-18
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2014-12-20
      • 2017-02-02
      • 1970-01-01
      相关资源
      最近更新 更多