title: How to exclude all the data that fail validation laravel?
body: <p>I only want the data that pass the validation. And do not want any errors or redirection using <code>laravel-validation</code>. So far I've been able to stop the redirection using a <code>laravel-request</code> but the <code>validation</code> does not seem to work properly if I leave the <code>failedValidation</code> method to not <code>throw</code> any <code>exception</code>.</p>
  
  <p>I have array of data from <code>user</code> who can fill the rows or leave them empty. If they leave the required fields empty in a row then I want to exclude them from the final data after <code>validation</code>.</p>
  
  <p><strong>ExpenditureExtry.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Requests;
  
  use Illuminate\Foundation\Http\FormRequest;
  use Illuminate\Contracts\Validation\Validator;
  use Illuminate\Http\Exceptions\HttpResponseException;
  
  class ExpenditureEntry extends FormRequest
  {
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
      return true;
    }
  
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
          'date' =&gt; 'required',
          'description.*' =&gt; 'nullable',
          'account_head.*' =&gt; 'required',
          'amount.*' =&gt; 'required_with:account_head.*'
        ];
    }
  
    /**
     * Failed validation disable redirect
     *
     * @param Validator $validator
     */
    protected function failedValidation(Validator $validator)
    {
      //throw new HttpResponseException(response()-&gt;json($validator-&gt;errors(), 422));
    }
  
  }
  </code></pre>
  
  <p><strong>Inside a controller</strong></p>
  
  <pre><code>public function store(ExpenditureEntry $req)
  {
     return $req-&gt;validated();
  }
  </code></pre>
  
  <p>I want this to return only those data that passed the validation. At the moment it returns all the data.</p>
  
  <p><strong>Expected output</strong></p>
  
  <pre><code>// http://127.0.0.1:8000/expenditure/add
  {
    "date": "03 Sep 2018",
    "description": {
      "1": null,
    },
    "account_head": {
      "1": "5",
    },
    "amount": {
      "1": "5000",
    }
  }
  </code></pre>
  
  <p><strong>Actual output</strong></p>
  
  <pre><code>// http://127.0.0.1:8000/expenditure/add
  {
    "date": "03 Sep 2018",
    "description": {
      "0": null,
      "1": null,
      "2": null,
      "3": "sas asd adas",
      "null": null
    },
    "account_head": {
      "0": "3",
      "1": "5",
      "2": null,
      "3": null,
      "null": null
    },
    "amount": {
      "0": null,
      "1": "5000",
      "2": "5000",
      "3": null,
      "null": null
    }
  }
  </code></pre>
  
  <p>I also tried using a <code>Validator::make</code> in the controller but it also doesnot work as expected:</p>
  
  <p><strong>Inside Controller</strong></p>
  
  <pre><code>public function store(Request $req)
  {
    $validator = Validator::make($req-&gt;except(['account_head.null', 'description.null', 'amount.null']), [
      'date' =&gt; 'required',
      'account_head.*' =&gt; 'required',
      'description.*' =&gt; 'nullable',
      'amount.*' =&gt; 'required_with:account_head.*'
    ]);
    return $validator-&gt;valid();
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>// http://127.0.0.1:8000/expenditure/add
  {
    "_token": "EmiRpnXhPHUPl3HIzuYLrtuLaGj9Ruv8AySe11Bp",
    "date": "03 Sep 2018",
    "description": [
      null,
      null,
      null,
      "sas asd adas"
    ]
  }
  </code></pre>
  
  <p><strong>PS: I want a <code>laravel</code> based solution if possible. If no such solution are possible, I actually have a solution to just manually validate them.</strong></p>
  
  <p><strong>My Solution</strong>
  <em>Went through every data and manually validated them. My problem with this approach is now I can't use other validation functions from laravel and when my fields increases, I don't think writing if condition will help me.</em></p>
  
  <pre><code>public function store(Request $req)
  {
    $datas = $req-&gt;except(['_token', 'account_head.null', 'description.null', 'amount.null']);
  
    $countR = count($datas['account_head']);
    $validated = [];
    $cv = 0;
    for ($i=0; $i &lt; $countR; $i++) {
      if($datas['account_head'][$i] == null) continue;
      else if($datas['amount'][$i] == null) continue;
  
      $validated['account_head'][$cv] = $datas['account_head'][$i];
      $validated['description'][$cv] = $datas['description'][$i];
      $validated['amount'][$cv] = $datas['amount'][$i];
      $cv++;
    }
  
    return $validated;
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>// http://127.0.0.1:8000/expenditure/add
  {
    "account_head": [
      "5"
    ],
    "description": [
      null
    ],
    "amount": [
      "5000"
    ]
  }
  </code></pre>