title: Laravel 5: Updating Multiple Records
body: <p>I have a <strong>Simple Todo App</strong>, that doesn't persist the update of multiple records at once. The client version works properly here: <a href="http://codepen.io/anon/pen/bVVpyN" rel="nofollow noreferrer">http://codepen.io/anon/pen/bVVpyN</a></p>
  
  <p>Everytime I make an action, I fire a http request to my laravel API to persist the data. But I have trouble with my <strong>completeAll method</strong>.</p>
  
  <p>Whenever I click the <strong>Complete All</strong> button, I execute the <strong>completeAll method</strong>:</p>
  
  <pre><code>completeAll: function () {
      var tasks = [];
      var taskIds = [];
  
      this.tasks = this.tasks.filter(function (task) {
          task.completed = true;
          tasks.push(task);
          taskIds.push(task.id);
          return task.completed;
      });
  
      this.$http.put('api/tasks/' + taskIds, { tasks: tasks });
  },
  </code></pre>
  
  <p>Here I send a request to an URL that might look like this:</p>
  
  <pre><code>http://localhost:8000/api/tasks/1,3,8,4
  </code></pre>
  
  <p>And together with it, I send the <strong>tasks object</strong>. This is the request payload from chrome developer tools:</p>
  
  <pre><code>{tasks: [{id: 1, body: "One", completed: true, created_at: "2015-09-09 08:36:38",…},…]}
  </code></pre>
  
  <p><strong>Then</strong> on the serverside I have an <strong>update function</strong> in my <strong>TasksController</strong> that looks like this:</p>
  
  <pre><code>public function update(Request $request, $id)
  {
      // gives ["1", "2", "3"] instead of "1,2,3" a
      $taskIds = explode(",", $id);
  
      // what happens if I update more than one task at once
      // ** PROBLEMATIC SECTION **
      if (count($taskIds) &gt; 1) {
          $tasks = Task::whereIn('id', $taskIds)-&gt;get()-&gt;toArray();
          $newTasks = Input::get('tasks');
  
          var_dump($tasks);
          var_dump($newTasks);
  
          /** This Line does not Work **/
          Task::whereIn('id', $taskIds)-&gt;update(Input::get('tasks'));
  
          return 'update more than one task at once';
      }
  
      // what happens if I update only one task
      Task::where('id', $id)-&gt;update(Input::all());
  
      return 'update exactly one task';
  }
  </code></pre>
  
  <p>The above <strong>$tasks</strong> and <strong>$newTasks</strong> variable <strong>var_dump</strong> the same, because I use the <strong>toArray()</strong> method on the <strong>$tasks</strong> variable.</p>
  
  <p>But when I try to update the tasks I get this error:</p>
  
  <pre><code>preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
  </code></pre>
  
  <p>Not sure why. Since in my understanding <strong>Task::whereIn('id', $taskIds)</strong> is an object and not a string. </p>
  
  <p><strong>Alternative routes that I tried so far</strong></p>
  
  <ol>
  <li><p>Using a foreach/for loop, to loop through the $tasks array, which I couldn't make to work, because I had also to loop through the $newTasks array to assign them, like this:</p>
  
  <p>foreach ($tasks as $task) {
      for ($i=0; $i &lt; count($taskIds); $i++) { 
          Task::where('id', $task->id)->update($newTasks[$i])
      }
  }</p></li>
  <li><p>Creating another endpoint on my API to just complete every task, to an URL like this:</p>
  
  <p><a href="http://localhost:8000/api/tasks/complete-all" rel="nofollow noreferrer">http://localhost:8000/api/tasks/complete-all</a></p></li>
  </ol>
  
  <p>And then executing a <strong>completeAll</strong> method on my TasksController, where I set each task to completed. But I couldn't make it work, because I got a </p>
  
  <pre><code>Method not allowed exception
  </code></pre>
  
  <p>Not sure why, but I figured it is better to use my original method with an API like this: </p>
  
  <pre><code>api/tasks/1,2,50,1,3
  </code></pre>
  
  <p>Because it was suggested here: <a href="https://laracasts.com/discuss/channels/code-review/vuejs-delete-multiple-tasks-each-task-one-request" rel="nofollow noreferrer">https://laracasts.com/discuss/channels/code-review/vuejs-delete-multiple-tasks-each-task-one-request</a></p>
  
  <hr>
  
  <p>Please advice on my original attempt to help me make this simple TodoApp work.</p>
  
  <p>Alternatively, if it is for the better, please help me make the foreach/for loop work. Thanks.</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>According to an answer, it was suggested to follow point 2 from above and create an additional endpoint.</p>
  
  <p>These are my routes:</p>
  
  <p><a href="https://i.stack.imgur.com/yN3ho.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yN3ho.jpg" alt="enter image description here"></a></p>
  
  <p>I use a post method to the url:</p>
  
  <pre><code>http://localhost:8000/api/tasks/complete-all
  </code></pre>
  
  <p>Executed from my app.js throught this:</p>
  
  <pre><code>this.$http.post('api/tasks/complete-all');   
  </code></pre>
  
  <p>And this is my adapted TasksController:</p>
  
  <pre><code>public function completeAll(Request $request)
  {
      $tasks = Task::where('completed', false)-&gt;get();
  
      foreach ($tasks as $task) {
         Task::where('id', $task-&gt;id)-&gt;update(['completed' =&gt; true]);
      }
  
      return response()-&gt;json(['message' =&gt; 'All tasks completed']);
  }
  </code></pre>
  
  <p>The app persists the data now correctly, but it troubles me, that I am not really updating anything from the clientside. I am just executing serverside code, because I am not really passing any query.</p>
  
  <p>How would I solve the problem, using an URL like this:</p>
  
  <pre><code>http://localhost:8000/api/tasks/1,23,43,5
  </code></pre>
  
  <p>Thanks</p>