【问题标题】:How to Convert stdClass object to int in PHP如何在 PHP 中将 stdClass 对象转换为 int
【发布时间】:2020-05-29 12:35:06
【问题描述】:

我想用 user_id 搜索我的数据库并获取金额值(int 类型)

$user = (int)(request('user'));
    $money = (int)(request('money'));
    $data = (DB::select("select amount from user_check where user_id = '$user'"));
    $array = json_decode(json_encode($data), True);
    foreach ($data as $value) 
    {
        $array[] = $value->amount;
    } 
    if($array[0]>$money)
    {
        return response(['user ok'=>$array[0],'money'=>$money]);
    }
    else
    {
        //return response(['user'=>$user,'Not enough money'=>$data]);
        return response('user not ok');
    }

但它不起作用,它总是进入 if nomather $money 有多大

【问题讨论】:

  • 当你只检查数组的第一个元素时,为什么还要费心制作数组?
  • @Sherif 我该怎么办?
  • 我不知道。你打算在这里发生什么?
  • var_dump($array[0]);Log::info($array[0]); 会得到什么?
  • @Sherif 我想检查一下,如果钱大于金额,则显示并保存(金额 - 钱)

标签: php mysql laravel api


【解决方案1】:

您最好更改查询,以便只获得您正在寻找的用户的价值。

$amount = DB::table('user_check')->where('user_id', $user)->pluck('amount');

或者如果 amount 是一个数组而不是单个值(取决于版本)

$amount = DB::table('user_check')->where('user_id', $user)->value('amount');

$user = DB::table('user_check')->where('user_id', $user)->first();
$amount = $user ? $user->amount : 0;

【讨论】:

  • pluck() 不会返回一个数组吗?我记得 - 从单行获取单个值是 ->value('columnName')
  • 我试图记住版本之间的区别。我有 4.2,其中 pluck 返回一个值。
  • 我认为他们将 pluck 重命名为 valuelists 重命名为 pluck。 V4.x 已经很老了 :-)
  • @PaulSpiegel 哈,我不知道吗。升级在我的待办事项清单上。我已经用另一种方法更新了答案。
  • 查看差异:laravel.com/docs/4.2/querieslaravel.com/docs/5.8/queries。它是 4.2 中的 pluck 和 5.8 中的 value
【解决方案2】:
如果值为“false”、“null”或“0”,

(int) 将返回 0。我敢打赌,如果请求输入未设置或格式错误,您不希望使用该查询来搜索 ID 为 0 的用户。

让我们清理代码:

$userId = request()->get('user');
$money = (int) request()->get('money'); 
# (int) here will be 0 if the request('money') is not sent with the request
# so it makes sense to have it here.

$userCheck = DB::table('user_check')->select('amount')
            ->where('user_id', $userId)
            ->first();

# ->first(); if you want to return a single row item
# ->get(); if you want to return multiple items, this one will return a collection (array) 
# and you need to treat that accordingly using foreach, or any array function

为什么需要$array = json_decode(json_encode($data), True);

if($userCheck->amount > $money){
    return response(['user ok'=> $userCheck,'money'=>$money]);
}else{
    return response('user not ok');
}

现在我假设您使用 (int) 来获取已发送数据的整数值,那里没有实际需要,并且在验证的情况下,您需要像这样单独验证:

如果它在控制器中

$this->validate($request,[
    'user'=>'required|exists:user_check,id',
    'money'=>'integer'
]);

如果您想在其他地方进行验证,您可以:

$validator = Validator::make($request->all(), [
    'user'=>'required|exists:user_check,id',
    'money'=>'integer'
]);

# Check if it fails
if($validator->fails()){
   # Validator fails, stop and return the errors instead
   return response()->with($validator->getMessageBag());
}

# This will validate the request and make sure that it has 'user'
# And that 'user' exists in the `user_check` table with that ID
# and that 'money' is an integer

如果您愿意验证,可以将该代码放在 DB::table()... 上方

希望能解决你的问题,告诉我怎么回事。

【讨论】:

    猜你喜欢
    • 2013-10-29
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多