F
Filament2y ago
Hro

Getting the attached and parent record in a relation manager hook

I know there is a way, I just cannot seem to directly figure out which one.
Tables\Actions\AttachAction::make()->after(function () {
dd('attached');
}),
Tables\Actions\CreateAction::make()->after(function () {
dd('Created');
}),
Tables\Actions\AttachAction::make()->after(function () {
dd('attached');
}),
Tables\Actions\CreateAction::make()->after(function () {
dd('Created');
}),
In the hooks above, how can I get the attached record and the record it was attached to?
6 Replies
Dan Harrin
Dan Harrin2y ago
$record is attached / created, $livewire->ownerRecord is the owner
Hro
HroOP2y ago
$record I believe I cannot get when attaching (User $record works fine for create after). I am doing this for now:
Tables\Actions\AttachAction::make()->after(function ($data, UsersRelationManager $livewire) {
$user = User::find($data['recordId']);
$unit = $livewire->ownerRecord;
}),
Tables\Actions\AttachAction::make()->after(function ($data, UsersRelationManager $livewire) {
$user = User::find($data['recordId']);
$unit = $livewire->ownerRecord;
}),
black ka1ser
black ka1ser2y ago
hello, quick question: can i use the after() method for detachaction as well?
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) {
QuizQuestion::where([
'quiz_id' => $livewire->ownerRecord->id,
'question_id' => $questionId,
])->delete();
}
}
),
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) {
QuizQuestion::where([
'quiz_id' => $livewire->ownerRecord->id,
'question_id' => $questionId,
])->delete();
}
}
),
doesnt seems to work for me but for attach it does work, except i use create() instead of delete()
Dan Harrin
Dan Harrin2y ago
which bit doesnt work?
black ka1ser
black ka1ser2y ago
its okay, i figured it out, some mistakes from my part
Tables\Actions\DetachAction::make()
->before(
function (RelationManager $livewire, $record) {

$questionIds = Topicable::where([
['topic_id', $record->id],
['topicable_type', 'Harishdurga\LaravelQuiz\Models\Question'],
])->pluck('topicable_id');

foreach ($questionIds as $questionId) {
QuizQuestion::where([
'quiz_id' => $livewire->ownerRecord->id,
'question_id' => $questionId,
])->update([
'deleted_at' => now()
]);
}
}
),
Tables\Actions\DetachAction::make()
->before(
function (RelationManager $livewire, $record) {

$questionIds = Topicable::where([
['topic_id', $record->id],
['topicable_type', 'Harishdurga\LaravelQuiz\Models\Question'],
])->pluck('topicable_id');

foreach ($questionIds as $questionId) {
QuizQuestion::where([
'quiz_id' => $livewire->ownerRecord->id,
'question_id' => $questionId,
])->update([
'deleted_at' => now()
]);
}
}
),
i should be using before() and $record
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server