0

I am studying Laravel 8 and trying to make a form validation and I want to know whether my $_POST is right? Or is there a better way to do this or do I have problems with my code?

See my code below:

<?php
 namespace App\Http\Controllers ;

 use illuminate\Http\Request ;
 


 class customers extends Controller {

    public function getData (Request $request){

        $name = $_POST['name']; // <--- is this right ?

    }
       
 }
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Lucas Souza
  • 5
  • 1
  • 1

2 Answers2

0
<?php
 namespace App\Http\Controllers;

 use Illuminate\Http\Request;



class customers extends Controller {

    public function getData (Request $request){

    // $name = $_POST['name']; // <--- this is not a right method to get any value in laravel we have to using Illuminate\Http\Request; for get any post and get value

    $name = $request->name;

 }
   
}

here is the document link for check doc

Developer
  • 466
  • 7
  • 18
0

Laravel has built-in functionality for working with input data.

https://laravel.com/docs/8.x/requests#input

In your case, you'd want to do:

$name = $request->input('name');

Among other things, this'll protect you from the "Undefined index" errors you'd get if the name field is missing.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368