['required', 'string', 'max:30'], 'body' => ['required', 'string', 'max:50'], 'role_ids' => ['required', 'array', 'min:1'], 'role_ids.*' => Rule::exists('roles', 'id'), ]; } public function validationAttributes(): array { return [ 'title' => 'judul', 'role_ids' => 'audiensi', 'role_ids.*' => 'audiensi', ]; } public function setBroadcast(Broadcast $broadcast) { $this->title = $broadcast->title; $this->body = $broadcast->body; $this->role_ids = $broadcast->roles->pluck('id')->toArray(); } public function store() { $this->validate(); DB::transaction(function () use (&$broadcast) { $broadcast = Broadcast::create([ 'user_id' => auth()->id(), 'title' => $this->title, 'body' => $this->body, ]); $broadcast->audiences()->attach($this->role_ids); }); return [ 'status' => true, 'data' => [ 'title' => $broadcast->title, 'body' => $broadcast->body, 'audience_ids' => $this->role_ids, ], ]; } public function update() { $this->validate(); DB::transaction(function () { $this->broadcast->update([ 'title' => $this->title, ]); $this->broadcast->audiences()->sync($this->role_ids); }); } }