-
app/Rules/BelongsToUserRule.php
Open in GitHubuse DB; use Illuminate\Contracts\Validation\Rule; class BelongsToUserRule implements Rule { private $table; private $item_id; private $user_id; private $message; public function __construct($table, $item_id, $user_id, $message) { $this->table = $table; $this->item_id = $item_id; $this->user_id = $user_id; $this->message = $message; } public function passes($attribute, $value) { return DB::table($this->table) ->where(['id' => $this->item_id, 'user_id' => $this->user_id]) ->exists(); } public function message() { return $this->message; } }
-
app/Http/Requests/Categories/DeleteCategoryRequest.php
Open in GitHubuse App\Rules\BelongsToUserRule; use Illuminate\Foundation\Http\FormRequest; class DeleteCategoryRequest extends FormRequest { public function rules() { return [ 'id' => [ 'required', 'integer', 'exists:categories', new BelongsToUserRule('categories', $this->id, auth()->id(), 'Эта категория не пренадлежит вам') ] ]; } public function authorize() { return true; } }