Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yumitdemir's avatar

Question About Using mutateFormDataBeforeCreate and API Requests in Filament with Laravel

Hi, I am new to Filament and Laravel in general. I have a question regarding automatically saving the user_id in a model that is associated with the User model, specifically when creating a LeaveRequest model. I want to auto-fill the user_id field with the ID of the logged-in user.

For instance, in my LeaveRequest model, I want the user_id to automatically populate when a new leave request is created. I achieved this using mutateFormDataBeforeCreate, and it works when submitting the form through the UI.

However, my question is: if I make an API request and change the user_id to someone else’s ID, would this still trigger the mutateFormDataBeforeCreate function?

To address this, I created a boot function in the model to manually set the user_id before saving the model. Is this the correct approach, or is there a better way to handle this.

0 likes
1 reply
LaryAI's avatar
Level 58

To ensure that the user_id is always set to the ID of the currently authenticated user, regardless of whether the request is coming from the UI or an API, you can use Eloquent model events. This approach is more reliable than relying solely on mutateFormDataBeforeCreate, which is specific to Filament forms and might not be triggered during API requests.

Here's how you can achieve this using the creating event in your LeaveRequest model:

  1. Define the creating event in your model:

    You can use the boot method to define a model event that sets the user_id before the model is saved to the database.

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Support\Facades\Auth;
    
    class LeaveRequest extends Model
    {
        protected static function boot()
        {
            parent::boot();
    
            static::creating(function ($leaveRequest) {
                // Ensure the user_id is set to the currently authenticated user's ID
                if (Auth::check()) {
                    $leaveRequest->user_id = Auth::id();
                }
            });
        }
    }
    
  2. Explanation:

    • Auth::check(): This checks if a user is authenticated. It's a good practice to ensure that the user is logged in before trying to access their ID.
    • Auth::id(): This retrieves the ID of the currently authenticated user.
    • creating event: This event is fired before a model is created, making it an ideal place to set default values or enforce certain constraints.
  3. Benefits:

    • Consistency: This approach ensures that the user_id is always set correctly, regardless of how the model is created (UI, API, etc.).
    • Security: It prevents unauthorized users from setting the user_id to someone else's ID, as it always uses the ID of the authenticated user.

By using the creating event, you ensure that the user_id is always set correctly, providing a consistent and secure way to handle this requirement across different types of requests.

Please or to participate in this conversation.