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

earmsby's avatar

Can't factory a model in Tinker

Trying to create a factory on a model in tinker (using command line in PHPStorm). I get the error:

 InvalidArgumentException  Source directory /tmp does not exist or is not a directory.

I tried the same operation (different model) on another project and no error. Everything worked.

I've found some info that this error is due to a permissions issue, but I don't understand why it works for one project but not the other. FWIW, both projects are only on my local machine and in the same directory (Herd).

What should I look for to resolve this issue?

0 likes
6 replies
Glukinho's avatar

Show code you're running.

/tmp is default directory used by \Faker\Provider\File::file method; maybe you should specify a folder somewhere in factory.

earmsby's avatar

Here's the code I was trying to run:

 \App\Models\LibraryItem::factory()->create()

I ran this in another project without getting the error

\App\Models\Venue::factory()->create() 

It's almost as if one project is using the regular windows user and the other is using some different user. The only thing I can think of that might account for this is that I created the project with the Venue model via PowerShell but the one with the LibraryItem model (the project throwing the error) via the Herd interface. I assumed it was more or less the same, but perhaps that's not the case.

Since I only just started to work on the "LibraryItem" project, I may delete the whole thing and try creating it the same way as the other one to see if that solves the issue.

earmsby's avatar

I just deleted the project and created it again, this time using PowerShell instead of Herd and now there's no error when I factory a user model. So, maybe creating a new project via Herd uses some other settings that caused this issue.

LaryAI's avatar
Level 58

The error:

InvalidArgumentException  Source directory /tmp does not exist or is not a directory.

indicates that Laravel (or one of its dependencies) is trying to access the /tmp directory, but it either doesn't exist or your PHP process doesn't have permission to use it.

Why does it happen in one project but not the other?

  • Different projects may use different PHP versions or configurations.
  • One project may have a different .env or cache state.
  • File/directory permissions may differ between projects.

How to resolve:

  1. Check if /tmp exists:

    In your terminal, run:

    ls -ld /tmp
    

    If it does not exist, create it:

    sudo mkdir /tmp
    sudo chmod 1777 /tmp
    
  2. Check permissions: The /tmp directory should be world-writable and have the sticky bit set (mode 1777). If not, fix it:

    sudo chmod 1777 /tmp
    
  3. Check PHP configuration: Run:

    php -i | grep 'sys_temp_dir'
    

    If sys_temp_dir is set to a non-existent directory, either create that directory or update your php.ini to point to a valid one.

  4. Check your project’s cache: Sometimes, cached files can cause issues. Clear Laravel’s caches:

    php artisan config:clear
    php artisan cache:clear
    php artisan view:clear
    php artisan route:clear
    
  5. Check for custom factory paths: If you have customized your factory paths or are using packages that do, ensure those directories exist and are writable.

  6. Restart your terminal/IDE: Sometimes, especially when using PHPStorm or Herd, restarting your IDE or terminal can help reload environment variables and permissions.

Summary:
This is almost always a system-level issue with the /tmp directory. Ensuring it exists and has the correct permissions should resolve the error.

Example Fix:

sudo mkdir -p /tmp
sudo chmod 1777 /tmp

After this, try running your factory command in Tinker again:

php artisan tinker
>>> \App\Models\YourModel::factory()->create();

If the error persists, double-check your PHP configuration and project-specific settings.

Please or to participate in this conversation.