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.