*/ public function rules(): array { return [ // `ids.*` costs one exists() query per element; the UI never selects // more than a page, and anything larger is what `all=true` is for. 'ids' => ['required_without:all', 'array', 'max:1000'], 'ids.*' => ['integer', 'exists:products,id'], 'all' => ['sometimes', 'boolean'], ...$this->targetRules(), ]; } /** * The rows this selection names: every filter match when `all` is set, * else the ids, each passed through writable(). * * @return Builder */ public function selection(): Builder { if ($this->boolean('all')) { $filters = FilteredProducts::fromRequest($this); // Live actions never serve a selection made in the trash: answered with // the live set, a bulk delete would destroy rows the user never saw. abort_if(($filters['trash'] ?? '') === 'only', 422, __('trash.bulk_wrong_target')); return self::writable(FilteredProducts::set()->apply([...$filters, 'trash' => ''])->reorder()); } return self::writable(Product::whereIn('id', Ids::toIntList((array) $this->input('ids', [])))); } /** * The same selection inside the trash: a separate method, as the defaults are opposite. * * @return Builder */ public function trashedSelection(): Builder { if ($this->boolean('all')) { return self::writable(FilteredProducts::set() ->apply([...FilteredProducts::fromRequest($this), 'trash' => 'only']) ->reorder()); } return self::writable(Product::onlyTrashed()->whereIn('id', Ids::toIntList((array) $this->input('ids', [])))); } /** What the selection names, for the audit entry: the ids, or the filter when `all`. */ public function target(): BulkTarget { return $this->boolean('all') ? BulkTarget::matching(FilteredProducts::fromRequest($this)) : BulkTarget::ids((array) $this->input('ids', [])); } /** * Rules for the value this action writes, if any. * * @return array */ protected function targetRules(): array { return []; } /** * Narrowed to the rows the caller may change: declare `scopeWritable()` * on the model to narrow every bulk action to it. * * @param Builder $query * @return Builder */ private static function writable(Builder $query): Builder { return $query->hasNamedScope('writable') ? $query->scopes(['writable']) : $query; } }