after() method of DetachAction is not working
for AttachAction it is working:
->headerActions([
Tables\Actions\AttachAction::make()
->label('Attach topic')
->after(
function (RelationManager $livewire, $data) {
// save all the questions of the attached topic into QuizQuestion
// 1. get all the questions of the topic, from Topicable
$questionIds = Topicable::where([
['topic_id', $data],
['topicable_type', 'Harishdurga\LaravelQuiz\Models\Question'],
])->pluck('topicable_id');
// 2. insert quizId and questionId into QuizQuestion
foreach ($questionIds as $questionId) {
QuizQuestion::create([
'quiz_id' => $livewire->ownerRecord->id,
'question_id' => $questionId,
]);
}
}
)
])
->headerActions([
Tables\Actions\AttachAction::make()
->label('Attach topic')
->after(
function (RelationManager $livewire, $data) {
// save all the questions of the attached topic into QuizQuestion
// 1. get all the questions of the topic, from Topicable
$questionIds = Topicable::where([
['topic_id', $data],
['topicable_type', 'Harishdurga\LaravelQuiz\Models\Question'],
])->pluck('topicable_id');
// 2. insert quizId and questionId into QuizQuestion
foreach ($questionIds as $questionId) {
QuizQuestion::create([
'quiz_id' => $livewire->ownerRecord->id,
'question_id' => $questionId,
]);
}
}
)
])
1 Reply
However for DetachAction it doenst go through the after() method:
->actions([
// make sure to remove the questions of a topic from QuizQuestion after detaching the topic
Tables\Actions\DetachAction::make()
->after(
function (RelationManager $livewire, $data) {
$questionIds = Topicable::where([
['topic_id', $data],
['topicable_type', 'Harishdurga\LaravelQuiz\Models\Question'],
])->pluck('topicable_id');
foreach ($questionIds as $questionId) {
$delete = QuizQuestion::where([
'quiz_id' => $livewire->ownerRecord->id,
'question_id' => $questionId,
])->delete();
dd($delete);
}
}
),
])
->actions([
// make sure to remove the questions of a topic from QuizQuestion after detaching the topic
Tables\Actions\DetachAction::make()
->after(
function (RelationManager $livewire, $data) {
$questionIds = Topicable::where([
['topic_id', $data],
['topicable_type', 'Harishdurga\LaravelQuiz\Models\Question'],
])->pluck('topicable_id');
foreach ($questionIds as $questionId) {
$delete = QuizQuestion::where([
'quiz_id' => $livewire->ownerRecord->id,
'question_id' => $questionId,
])->delete();
dd($delete);
}
}
),
])