Laravel 5 Validation Trim

后端 未结 8 922
梦谈多话
梦谈多话 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

    You can use this in your request file.

    $input = request()->all();
    $input['url'] = trim($input['url']);
    request()->replace($input);
    

    You can also create an all() function in your request file to update request parameters:

     public function all()
        {
            $all = parent::all(); // $this->all();
            $all['new_key'] = "new_data";
            $all['old_key'] = "new_data";
    
            return $all;
        }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题