Is usage of return value of laravel’s validate important?

let’s imagine this code.

public function store(Request $request) {
  $validated = $request->validate(['name' => 'required']);

  $post = new Post();

  $post->name = $request->name;
  // OR
  $post->name = $validated['name'];

  $post->save();
}

is it important to use $validated['name'] as opposed to $request->name?

i was testing some basic sql injections and my app seemed to be protected even with $request->name

so is $request validated?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As @Tim Lewis said they are the same

because if the validation didn’t pass it will not continue to the rest of your code inside the controller’s method

the most common case to use the validated array is when you try to create new model or update one and you want to add each property manually so you could just pass it directly like this

 $validated = $request->validate([
      'title' => 'required',
      'body' => 'required' 
]);

Post::create($validated);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x