New middlewares in Laravel 5.4 – TrimStrings and ConvertEmptyStringsToNull

Laravel 5.4 shipped with two new middlewares called TrimStrings and ConvertEmptyStringsToNull

TrimStrings middleware – automatically trim all the request data.
ConvertEmptyStringsToNull middleware- automatically convert all strings to null.

Trim Strings Middleware

As the name implies, this middleware removes extra spaces from the beginning and ending from request data automatically. For example, if a user submits something like this.

With the presence of this middleware (\Illuminate\Foundation\Http\Middleware\TrimStrings::class) in App/Kernel.php, it gets converted into.

Its removed extra space.

ConvertEmptyStringsToNull

Just as the name suggests, this middleware converts empty strings to null. If a user submits an empty form instead of getting '' it gets converted to null.

If the user submits a form with a not required field that maps to the nullable database column.

With the presence of this middleware (\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class) in App/Kernel.php, it gets converted into.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status