Laravel 5 Validation Trim

后端 未结 8 928
梦谈多话
梦谈多话 2020-12-18 05:23

I am a beginner in Laravel 5.

How can I remove whitespaces in validator?? i have read the documentation but there is no validator for trim(remove whitespaces).

8条回答
  •  天命终不由人
    2020-12-18 06:01

    $data = $r->all();
    foreach ($data as $key => $value) {
    if($value!=""){ //If your primaryKey id is autoincrement
       $data[$key] = preg_replace('/\s{2,}/',' ',$value);
    }
    }
    $variable = new modelName($data);
    $variable->save();
    
    //Here Parameter Explaination
     => '/\s{2,}/' Pattern must write between '/ /' and \s{2,} means 2 or more than 2 spaces sholud be trim
     => ' ' second parameter is with. Means 1st parameter replace with 2nd parameter(here, one space)
     => $value is variable which is trim
    

提交回复
热议问题