Accessing $request vars in observer

So I ran into this problem multiple times but always changed it differently. I'm now posting this hoping there is an easier solution. Sometimes I use fields in my form (for recurring event for example) that I don't need saved in the database. If I don't specify the fields in the fillable array (on the model) then they don't appear in the observer:
public function creating(Schedule $schedule): void
{
// recurrence_weekdays is set in the form but not in fillable
$schedule->recurrence_weekdays // this will be null
// if I try this:
$request = request();
$request->input('recurrence_weekdays');
// the result is also null, but if I check with dd the array the data is in there. It just looks like because it is send with livewire everything gets a bit weird.
}
public function creating(Schedule $schedule): void
{
// recurrence_weekdays is set in the form but not in fillable
$schedule->recurrence_weekdays // this will be null
// if I try this:
$request = request();
$request->input('recurrence_weekdays');
// the result is also null, but if I check with dd the array the data is in there. It just looks like because it is send with livewire everything gets a bit weird.
}
I know i should maybe do this with a: ->mutateFormDataUsing() But I would have to do that at multiple places (in the resource and livewire page with fullcalendar for example), and personally I love putting this logic in an observer so I always know where to look. So my question: how can I access $_POST vars (or $request->inputs) in an observer? Without adding them as a fillable field?
2 Replies
OzgurOzarpaci
OzgurOzarpaci3mo ago
I think your aproach is wrong. If you want to use a function in multiple places, you can use service pattern.
Sjoerd24
Sjoerd242mo ago
I think you are probably right. I am not familiar with service patterns. For now i think i will handle the logic in the resource and do the other part in the observers. Part of the work should anyways be done in a queue i think. I’m pretty new to laravel so still figuring out what the best way to structure everything is.