欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

删除 移除 request 参数 Validate or remove for extra fields in laravel Delete parameters from request 有大用

Delete parameters from request

PUBLISHED 6 MONTHS AGO BY WIZIX

Hey guys, I have this method allowing me to update the information of an user profile :

    public function update_info(UpdateProfileInfo $request, User $user)
    {
        if ($request->hasFile('picture'))
        {
            Storage::delete($user->picture); // Delete the picture if it already exists

            $ext = '.' . $request->file('picture')->extension();
            $user->picture = $request->file('picture')->storeAs('pictures', $user->id . $ext, 'public');

            $request->files->remove('picture'); // Delete the key, because we already manually changed the value
        }

        $user->update($request->all());

        return redirect()->route('profile.show', ['user' => $user]);
    }

If there is a picture uploaded, I manually update the model, delete the key and then update automaticly the other fields. But it doesn't work as expected because, before $user->update(), I have the right path for my picture and after the update, I have /tmp/phpmFOzBt. Why ?

Thanks !

Best Answer(As Selected By Wizix)
sutherland

Look into using $request->only([]) instead of ->all() so that you only update the fields you want and don't overwrite the file path.

mikevrind

Files get uploaded to the tmp directory by default. You should always move those files to a different location.

Visit https://laravel.com/docs/5.5/filesystem#file-uploads for more information.

Afterwards you could always update or overwrite the request data with the new file path. Another option is to use $request->except('picture') and add that value manually to the update method with the correct path.

sutherland

Look into using $request->only([]) instead of ->all() so that you only update the fields you want and don't overwrite the file path.

Please sign in or create an account to participate in this conversation.


来自  https://laracasts.com/discuss/channels/laravel/delete-parameters-from-request




It's posible to validate request with rules for additional fields, or remove that fields from request?

Simple example, I have FormRequest object with rules:

public function rules() {
        return [
            'id' => 'required|integer',
            'company_name' => 'required|max:255',
        ];
    }

And when I get post request with any other fields I want to get error/exception in controller or I want to get only id and company_name fields, with no any others. It's exist any feature in laravel with that, or I must make it in my way?



It's posible to validate request with rules for additional fields, or remove that fields from request?

Simple example, I have FormRequest object with rules:

public function rules() {
        return [
            'id' => 'required|integer',
            'company_name' => 'required|max:255',
        ];
    }

And when I get post request with any other fields I want to get error/exception in controller or I want to get only id and company_name fields, with no any others. It's exist any feature in laravel with that, or I must make it in my way?

    Your post request with laravel has more stuff than just your form input so you need to check Request to see what is inside.

    To get only the fields you want you can use:

    $request->only(['fieldname1', 'fieldname2', 'fieldname3']);

    or

    $request->except(['fieldnameYouDontWant1', 'fieldnameYouDontWant2', 'fieldnameYouDontWant3']);

    to exclude the ones you don't want.

      You can use except method of request which will give you fields except for the given fields

      $request = $request->except('your_field');

      if you want to remove multiple fields from request you can pass array of the fields

      $request = $request->except(['field_1','field_2',...])

      Reference: Laravel requests

      Things get a little trickier when you start getting into nested array element validations, especially if wildcards are involved. Put this in your custom Request class, and inject it into your controller method. Should cover all cases.

      public function authorize()
      {
          //Dot notation makes it possible to parse nested values without recursion
          $original = array_dot($this->all());
          $filtered = [];
          $rules = collect($this->rules());
          $keys = $rules->keys();
          $rules->each(function ($rules, $key) use ($original, $keys, &$filtered) {
              //Allow for array or pipe-delimited rule-sets
              if (is_string($rules)) {
                  $rules = explode('|', $rules);
              }
              //In case a rule requires an element to be an array, look for nested rules
              $nestedRules = $keys->filter(function ($otherKey) use ($key) {
                  return (strpos($otherKey, "$key.") === 0);
              });
              //If the input must be an array, default missing nested rules to a wildcard
              if (in_array('array', $rules) && $nestedRules->isEmpty()) {
                  $key .= ".*";
              }
      
              foreach ($original as $dotIndex => $element) {
                  //fnmatch respects wildcard asterisks
                  if (fnmatch($key, $dotIndex)) {
                      //array_set respects dot-notation, building out a normal array
                      array_set($filtered, $dotIndex, $element);
                  }
              }
          });
      
          //Replace all input values with the filtered set
          $this->replace($filtered);
      
          //If field changes were attempted, but non were permitted, send back a 403
          return (empty($original) || !empty($this->all()));
      }

        I did it by override method validate() in FormRequest class.

        abstract class MyFormRequest extends FormRequest {
            public function validate() {
                foreach ($this->request->all() as $key => $value) {
                    if (!array_key_exists($key, $this->rules())) {
                        throw new ValidationException("Field " . $key . " is not allowed.");
                    }
                }
                parent::validate();
            }
        }

        I think it's not best way, but it's works. I should upgrade it to show info about all wrong fields at once :)

        You can do it through your controller. You can send only two or certain fields to database. Use $request->only() instead of $request->all() and pass a array of data you want to save in database. Your controller's store() method should look like this:

        public function store(Request $request)
        {
            ModelName::create($request->only(['id','company_name']));
            return redirect('RouteName');
        }

          I have a new solution for this, create a new function in your form request class:

          public function only_in_rules(){
              return $this->only(array_keys($this->rules()));
          }

          Then in your controller, call $request->only_in_rules() insteads of $request->all()

          To remove key from request in Laravel, use:

          $request->request->remove('key')

            Starting form Laravel 5.5 you can call validate method right on Request e.g.

            class YourController extends Controller
            {
            
                public function store(Request $request) {
            
                  $cleanData = $request->validate([
                    'id' => 'required|integer',
                    'company_name' => 'required|max:255',
                  ]);
            
                  // Now you may safely pass $cleanData right to your model
                  // because it ONLY contains fields that in your rules e.g
            
                  $yourModel->create($cleanData);
              }
            }

            Your Answer

            来自  https://stackoverflow.com/questions/35327679/validate-or-remove-for-extra-fields-in-laravel

            普通分类: