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

lara28580's avatar

How to store the user_id in my client relation

I am having a hasmany relation between user and client and I want to store the user_id on the clients table. What I have

class ClientResource extends Resource
{
    protected static ?string $model = Client::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                TextInput::make('name')->required()->columnSpan(['default' => 2, 'sm' => 1]),
                TextInput::make('email')->email()->required()->columnSpan(['default' => 2, 'sm' => 1]),
                TextInput::make('phone')->required()->columnSpan(['default' => 2, 'sm' => 1]),
                TextInput::make('address')->required()->columnSpan(['default' => 2, 'sm' => 1]),
                TextInput::make('city')->required()->columnSpan(['default' => 2, 'sm' => 1]),
                TextInput::make('state')->required()->columnSpan(['default' => 2, 'sm' => 1]),
                TextInput::make('zip')->required()->columnSpan(['default' => 2, 'sm' => 1]),
                TextInput::make('country')->required()->columnSpan(['default' => 2, 'sm' => 1]),
                Textarea::make('notes')->rows(10)->columnSpan(2)->required()->columnSpan(['default' => 2]),
            ])->columns(2)->model(auth()->user());
    }

How can I pass the user id so I could store it on the clients table?

0 likes
6 replies
LaryAI's avatar
Level 58

To store the user_id in the clients table, you need to ensure that the user_id is a fillable attribute in your Client model and then set it when you are creating a new client. Here's how you can modify your ClientResource to include the user_id:

class ClientResource extends Resource
{
    protected static ?string $model = Client::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                // ... other fields ...
                Hidden::make('user_id')->default(fn () => auth()->id()), // Add a hidden field for user_id
            ])
            ->columns(2);
    }
}

In the Client model, make sure to add user_id to the $fillable array:

class Client extends Model
{
    protected $fillable = [
        'name',
        'email',
        'phone',
        'address',
        'city',
        'state',
        'zip',
        'country',
        'notes',
        'user_id', // Ensure this is fillable
    ];

    // ... other model methods ...
}

This way, when the form is submitted, the user_id will be automatically filled with the ID of the currently authenticated user. The Hidden::make() method creates a hidden input field in the form, and the default() method sets the default value for this field using a closure that returns the ID of the authenticated user.

Remember to import the Hidden class at the top of your ClientResource file:

use Filament\Forms\Components\Hidden;

Now, when you create a new client using this form, the user_id will be set to the ID of the currently logged-in user and stored in the clients table.

cms007's avatar

In your CreateClient class, add a mutator method like this:

protected function mutateFormDataBeforeCreate(array $data): array
{
    $data['user_id'] = auth()->id();

    return $data;
}

And obviously make the user_id fillable under Client model.

That's it!

1 like
lara28580's avatar

@cms007 Thanks for you answer I did it via observer and it works perfectly.

sayedsadat's avatar

I have user Observer

    $post->user()->associate(auth()->user());

as well as

   protected function mutateFormDataBeforeCreate(array $data): array
   {
        $data['user_id'] = auth()->id();

        return $data;
   }

Non of them are working with the fillament model (if you are using model instead of the /create page).

Note: these methods do work with create action if it is from createPage (/create).

Please or to participate in this conversation.