. */ namespace App\Commands; use App\Models\Announcement; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Validator; class CreateAnnouncementCommand extends CommandBase { /** @var Announcement */ private $_announcementName; public function __construct($announcementName) { $this->_announcementName = $announcementName; } /** * @return bool */ public function authorize() { return Gate::allows('create-announcement'); } /** * @throws \Exception * @return CommandResponse */ public function execute() { $rules = [ 'name' => 'required|max:50', ]; $validator = Validator::make([ 'name' => $this->_announcementName, ], $rules); if ($validator->fails()) { return CommandResponse::fail($validator); } Announcement::create([ 'title' => $this->_announcementName, ]); return CommandResponse::succeed(['message' => 'Announcement created!']); } }