/** * Bulk delete the specified resources in storage. * * @param string $action to perform * @param string|array $ids * @return integer count of actions performed */ public function bulkAction($action, $ids) { // Get a collection of resources $collection = Collection::parseMixed($ids, [',', '+', ' ']); // Get any extra arguments passed $arguments = array_slice(func_get_args(), 2); // Run in a transaction for all or nothing behavior DB::transaction(function () use($collection, $action, $arguments) { // Iterate over the resources performing the action on each $collection->each(function ($id) use($action, $arguments) { // Repository@<action>($id) array_unshift($arguments, $id); call_user_func_array([$this, studly_case($action)], $arguments); }); }); // Fire bulk after event listeners $this->eventFire('bulk.' . snake_case($action), $collection->all()); return $collection->count(); }
/** * Display a confirmation modal for the specified resource bulk action. * * @param string $action * @param string|array $ids (optional) * @return Illuminate\View\View */ public function confirmBulk($action, $ids = null) { $data = []; $inTrash = false; // Get the objects from the repository $ids = is_null($ids) ? Input::get('ids') : $ids; if (!is_null($ids)) { $ids = Collection::parseMixed($ids, [',', '+', ' '])->all(); // Get the resource out of the trash if (in_array($action, $this->trashableActions) && method_exists($this->getRepository(), 'retrieve')) { $inTrash = true; } // Get the resources as a collection $collection = $this->getRepository()->findIn('id', $ids); // Pass collection under the plural package named variable to the view $data = [str_plural($this->package) => $collection, 'ids' => $ids]; } // Render confirmation modal $view = str_replace('bulk_', 'bulk.', $action) . '_confirm'; return $this->modal($view, $data); }
/** * Add a scope filter. * * @param string $name of scope closure * @param mixed $args to pass to closure * @return void */ public function addScope($name, $args) { // Make sure that scopes match the filters $scopes = isset($this->filters['scopes']) ? $this->filters['scopes'] : []; $this->scopes = array_merge($this->scopes ?: [], $scopes); // Convert mixed to array $args = Collection::parseMixed($args, [','])->all(); $args = array_values($args); $arrs = array_filter($args, function ($arg) { if (!is_numeric($arg) && !is_bool($arg) && empty($arg)) { return false; } return true; }); // Only add the scope if the args are not empty if (!empty($args) && $args == $arrs) { $this->scopes[$name] = $arrs; $this->filters['scopes'] = $this->scopes; } }