Laravel:检查 MySQL 查询是否有结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42589861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:28:00  来源:igfitidea点击:

Laravel: Check if MySQL query has result

phpmysqllaravellaravel-5

提问by Feralheart

First Laravel project. I have a controllerfunction what checks if there any record with that barcode. If no insert a record. If yes add one for the count.

第一个 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);

}

}

When I tried it, it had the following error:

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

ErrorException in SellController.php line 17: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given

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

What did I wrong?

我做错了什么?

回答by Jerodev

You can't just just mysql_num_rowson a Laravel query builder. Laravel query builder will return a collection, so you can just use the isEmptyfunction to find out if it has any results.

你不能只mysql_num_rows在 Laravel 查询构建器上。Laravel 查询生成器将返回一个集合,因此您可以使用该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]);
}

If you are using a Laravel version pre-5.3, the query builder will return an array. In this case, you can use the php countfunction on this array to know how many rows are returned

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

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]);
}

回答by Ulrik McArdle

It would be a good idea to create a model and use that to query the db. Maybe something like this (in my opinion it is way easier):

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

$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');
}

回答by user8128167

When I used test->isEmpty()or count($test) == 0I still kept getting this error:

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

Undefined offset: 0

未定义的偏移量:0

So I ended up using this to check for an empty return value from DB::select:

所以我最终使用它来检查来自 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');                
}