Redirect from a Create Page back to where it came from

I want to redirect from a Create Page which I triggered in the following way:
CreateAction::make()
->url(fn ($livewire) => VacancyClassificationResource::getUrl('create', ['ownerRecord' => $livewire->ownerRecord->getKey()]))
CreateAction::make()
->url(fn ($livewire) => VacancyClassificationResource::getUrl('create', ['ownerRecord' => $livewire->ownerRecord->getKey()]))
This CreateAction is inside my VacancyClassificationRelationManager which lives on my VacancyResource. I want to redirect back to my Vacancy Edit screen after I created a record. How and where can I do this?
22 Replies
Dennis Koch
Dennis Koch2y ago
Not sure which one you want. You mention CreatePage and Relation Manager? 🤔 Oh I see, you redirect to the CreatePage from the RM.
Daniel Plomp
Daniel PlompOP2y ago
Yes, so I'm on an Edit Page, which of course is filtered. If I click 'Cancel' from the Create Page, I get back to the right page. What redirect action is that?
Dennis Koch
Dennis Koch2y ago
It's the CancelAction
Daniel Plomp
Daniel PlompOP2y ago
Yes, I understand 🙂 Just, how can I call that action from this redirectUrl() method? Eh... this one getRedirectUrl()
Dennis Koch
Dennis Koch2y ago
I don't think you can/should call actions directly. Check the source code of the CancelAction if you want the code for the redirect. Should be a simple redirect()->back() or similar
Daniel Plomp
Daniel PlompOP2y ago
OK. Thanks. Nothing seems to work anyways. If I create a new record and click 'Create', then the record is saved and I'm not being redirected but all form values are cleared and I can create a new record.
josef
josef2y ago
Have you tried the after() method on the CreateAction to redirect to where you want to go?
AlexAnder
AlexAnder2y ago
protected function getActions(): array
{
return [
Action::make('create')
->action(function (array $data) {
$source = Source::create([
'type_id' => $data['type_id']
]);

return redirect()->route('filament.resources.source.edit', $source->id);
})
]
}
protected function getActions(): array
{
return [
Action::make('create')
->action(function (array $data) {
$source = Source::create([
'type_id' => $data['type_id']
]);

return redirect()->route('filament.resources.source.edit', $source->id);
})
]
}
Something like that
Daniel Plomp
Daniel PlompOP2y ago
Hmm.. not yet. I'll try that. Maybe I'm sticking with just showing the modal window. The only thing is that I actually want to save multiple entries based on the values of a CheckboxList inside the modal window. Is something like that also possible? So that I stop the default Save Action and manipulate the data myself?
Dan Harrin
Dan Harrin2y ago
manipulating the data in RMs is in the docs
Daniel Plomp
Daniel PlompOP2y ago
But can I cancel the CreateAction and then do my own thing? I tried this:
->headerActions([
CreateAction::make()
->before(function (array $data, CreateAction $createAction) {

// Save the multiple selected Id's from the CheckboxList into
// multiple entries

// ray($data);
// ray()->pause();

// Cancel the action, since we don't want to save the 'default' entry
$createAction->cancel();

})
])
->headerActions([
CreateAction::make()
->before(function (array $data, CreateAction $createAction) {

// Save the multiple selected Id's from the CheckboxList into
// multiple entries

// ray($data);
// ray()->pause();

// Cancel the action, since we don't want to save the 'default' entry
$createAction->cancel();

})
])
Dan Harrin
Dan Harrin2y ago
you can override action() function instead to replace the default behaviour check the internal CreateAction class, you can see how we built it
Daniel Plomp
Daniel PlompOP2y ago
Owke... I need to dive into that 🙂 So basically you could override the CreateAction, get the Model data and do your own thing with that? Any example of how to override the action to begin with?
Dan Harrin
Dan Harrin2y ago
you just chain on ->action() like you did ->before() no need to create a new class. i was just telling you to look into CreateAction to see what the default configuration was
Daniel Plomp
Daniel PlompOP2y ago
Yes, that is clear. The following code works, but it feels a bit cumbersome. As if this could be done differently?
CreateAction::make()
->action(function (array $data, $livewire) {

// Save the multiple selected Id's from the CheckboxList into multiple entries

// First get the Vacancy
$vacancy_id = $livewire->ownerRecord->id;
$vacancy = Vacancy::find($vacancy_id);

// Loop through the VacancyTypeClassifications
foreach ($data['vacancy_type_classification_ids'] as $id) {
$vacancyClassification = new VacancyClassification([
'vacancy_id' => $vacancy->id,
'vacancy_type_classification_id' => $id
]);

ray($vacancyClassification);
ray()->pause();

// Save the records
$vacancyClassification->save();
}
})])
CreateAction::make()
->action(function (array $data, $livewire) {

// Save the multiple selected Id's from the CheckboxList into multiple entries

// First get the Vacancy
$vacancy_id = $livewire->ownerRecord->id;
$vacancy = Vacancy::find($vacancy_id);

// Loop through the VacancyTypeClassifications
foreach ($data['vacancy_type_classification_ids'] as $id) {
$vacancyClassification = new VacancyClassification([
'vacancy_id' => $vacancy->id,
'vacancy_type_classification_id' => $id
]);

ray($vacancyClassification);
ray()->pause();

// Save the records
$vacancyClassification->save();
}
})])
AlexAnder
AlexAnder2y ago
I have action with closure and ->form([...]) // with multiple entry fields, then in closure insert/update, whatever and in final return redirect in the end redirect and yes..
Daniel Plomp
Daniel PlompOP2y ago
Well, the modal closes automatically, so no need for a redirect?
AlexAnder
AlexAnder2y ago
I redirect for edit new record
Dan Harrin
Dan Harrin2y ago
$livewire->ownerRecord is the vacancy, so why find() it again? then you can use $vacancy->classifications()->create(['vacancy_type_classification_id' => $id]) to create and save at once
CreateAction::make()
->action(function (array $data, $livewire) {
foreach ($data['vacancy_type_classification_ids'] as $id) {
$livewire->ownerRecord->classifications()->create([
'vacancy_type_classification_id' => $id,
]);
}
})])
CreateAction::make()
->action(function (array $data, $livewire) {
foreach ($data['vacancy_type_classification_ids'] as $id) {
$livewire->ownerRecord->classifications()->create([
'vacancy_type_classification_id' => $id,
]);
}
})])
Daniel Plomp
Daniel PlompOP2y ago
Thanks. That is indeed a better way.
Dan Harrin
Dan Harrin2y ago
you can probably improve it using createMany()
Want results from more Discord servers?
Add your server