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

kyort's avatar
Level 2

Filament importer is not including all of my columns

I am trying to load some agencies from a csv. Among their data is as 'Case Number' column which is unique. This case number is going to be the username in the Users table. I have uploaded the same csv file into the Users table to get the users created. I am now trying to load it into a Providers table which has a foreignIdFor(User:class) field.

I have this importColumn defined:

			ImportColumn::make( 'user_id' )
                    ->relationship( resolveUsing: function( string $state): ?User {
                        return User::query()
                                   ->where( 'username', $state )
                                   ->first();
                    } ),

In my terminal all I see is a failed job.

		2024-10-17 20:54:42 Filament\Actions\Imports\Jobs\ImportCsv ........ RUNNING
		2024-10-17 20:54:42 Filament\Actions\Imports\Jobs\ImportCsv ... 56.24ms FAIL

My postgres logs show the query is not trying to insert into the user_id field at all.

      insert into "providers" ("signer", "license", "contract", "facility", "street", 
       "street2", "city", "state", "zip", "same_address", 
       "physical_street", "physical_street2", "physical_city", 
       "physical_state", "physical_zip", "email", "license_type", 
       "phone", "updated_at", "created_at") 
        values                                                      
         ('Owner Name', '##########, '#####', 'Facility Name', 
         'address line 1', null, 'City', 'State', 'Zip', 'false', 
         'physical address line 1', null, 'City', 'State', 'Zip', 
         '[email protected]', 'Licensed', '(###) ###-####', 
         '2024-10-17 19:55:22', '2024-10-17 19:55:22')

Have I screwed up in my importColumn definition? I tried to go directly off the documentation.

0 likes
1 reply
LaryAI's avatar
Level 58

It looks like the issue might be related to how the ImportColumn is being defined and used. Specifically, the relationship method should be used to define a relationship between the imported data and an existing model, but it seems like the resolveUsing function might not be correctly setting the user_id field in the Providers table.

Here are a few steps to troubleshoot and potentially resolve the issue:

  1. Ensure the CSV Column Matches: Make sure that the CSV column name that contains the 'Case Number' matches exactly with what you are using in your import logic. The resolveUsing function should be receiving the correct value from the CSV.

  2. Check the Relationship Definition: The relationship method should be used to define a relationship, but it seems like you are trying to resolve a user based on a username. Instead, you might want to use resolveUsing directly on the ImportColumn to set the user_id field.

  3. Set the user_id Directly: Instead of using relationship, you can directly set the user_id by resolving the user and returning the id. Here's how you can modify your import logic:

ImportColumn::make('user_id')
    ->resolveUsing(function (string $state): ?int {
        $user = User::query()
            ->where('username', $state)
            ->first();

        return $user ? $user->id : null;
    }),
  1. Check for Errors in the Job: Since the job is failing, check the logs for any specific error messages that might indicate why the job is not completing successfully. This could provide more insight into what might be going wrong.

  2. Database Constraints: Ensure that there are no database constraints (like foreign key constraints) that might be causing the insert to fail if the user_id is not being set correctly.

  3. Debugging: Add some logging inside the resolveUsing function to see if it's being called and what values are being returned. This can help you verify that the function is working as expected.

By ensuring that the user_id is correctly resolved and set, you should be able to import the data into the Providers table successfully. If the issue persists, consider checking the Filament documentation or reaching out to the community for further assistance.

Please or to participate in this conversation.