Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jonjie's avatar
Level 12

Security DB::raw() in laravel

Is it safe to use this kind of code?

0 likes
7 replies
Jonjie's avatar
Level 12

@tray2 It is an API, so basically the request comes from the current latitude and longitude of the user.

drewdan's avatar

Are you validating the input data? I think that any data coming from an outside source should always be validated first.

Jonjie's avatar
Level 12

@drewdan Yes. I have a validation section for this.

$rules = [
	'latitude' => 'required',
	'longitude' => 'required'
];

$validator = Validator::make(request()->all(), $rules);

if($validator->fails()){
return response()->json($validator->messages());
}
1 like
jlrdw's avatar

Numeric is more secure from sql injection. But it's good to bind all parameters. Just me personally I don't usually worry about things like int field and date fields.

But you could experiment a little try some SQL injection yourself, safe ones of course and see what works and what doesn't work.

But remember that data is string data coming in or in a post. PHP and MySQL automatically handle it when being saved whereas in some languages like C sharp you have to specifically cast those variables.

Please or to participate in this conversation.