Home » Php » mysql – How to Convert stdClass object to int in PHP

mysql – How to Convert stdClass object to int in PHP

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to search into my database with user_id and get the amount value(int type)

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

but it doesn’t work, it always going into the if nomather how big the $money is

How to&Answers:

You’d be better off changing the query so you only get the value of the user you’re looking for.

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

or if amount is an array instead of a single value (version dependent)

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

OR

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

Answer:

(int) will return 0 if the value is “false”, “null”, or “0”. And i bet you don’t want to have that query to search for a user with ID of 0 in case the request input is not set or malformed.

Let us clean that code:

$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

Why do you need $array = json_decode(json_encode($data), True); ?

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

Now i assume you’re using the (int) to get the integer value of sent data, there’s no actual need there, and in case of validation you need to validate that separately like so:

If it’s in controller

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

If you want to make the validation elsewhere you can:

$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

If you’re willing to validate you can put that code above the DB::table()...

I hope that solves your problem, let me know how it goes.