Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to differentiate request empty string and null on laravel

Tags:

laravel-5

in php we can do

isset($_POST['name'])

so that user submit empty text can be detected.

but in laravel

$request->has('name')

return null on empty string too.

I need to find a way to tell if it is an empty string or the field not posted at all

like image 273
Elliot Yap Avatar asked Mar 26 '17 07:03

Elliot Yap


2 Answers

If you're using laravel 5.4 or above, by default it uses a middleware to convert all empty strings to null.

In App\Http\Kernel.php: \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class

Comment out this middleware to prevent this behaviour.

like image 164
Joe Avatar answered Sep 28 '22 04:09

Joe


You can use exists() method in request instance. If you want to check if there is value in request check:

$request->has('key');

if you want to check if the key is exists use:

$request->exists('key');
like image 30
MoPo Avatar answered Sep 28 '22 05:09

MoPo