(int) вернет 0, если значение равно "false", "null" или "0". И я готов поспорить, что вам не нужен этот запрос для поиска пользователя с идентификатором 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()...
Я надеюсь, что это решит вашу проблему, дайте мне знать, как это происходит .