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

PT-83's avatar
Level 7

PHP Tinker Factory Error

Hi all, I'm tyring to create faker posts using tinker I get the following error message:

InvalidArgumentException with message 'Source and target directories must differ.'

Factory

public function definition()
    {
        return [
            'user_id' => \App\Models\User::factory(),
            'title' => $this->faker->sentence,
            'date' => $this->faker->date,
            'file' => $this->faker->file,
            'body' =>$this->faker->paragraph,
        ];
    }

In tinker I am running this command Post::factory()->count(5)->create();

Any idea on how to solve this?

0 likes
7 replies
PT-83's avatar
Level 7

@nakov thanks I looked at that thread and changed the code as follows:

 public function definition()
    {
        return [
            'user_id' => \App\Models\User::factory(),
            'title' => $this->faker->sentence,
            'date' => $this->faker->date,
            'file' => $this->faker->image('public/storage/img',400,300, null, false), 
            'body' =>$this->faker->paragraph,
        ];
    }

However, I am getting InvalidArgumentException with message 'Cannot write to directory "public/storage/img"' error message.

Looking at my public->storage folder I see the sym link arrow, but I don't see the img folder. Do I have to manually create the img folder?

Nakov's avatar

You don't need to write to the public/storage but directly to the storage folder, the symlink will make the image available to the public.. So use this:

$this->faker->image(storage_path('img'),400,300, null, false)
PT-83's avatar
Level 7

Same error is occuring when using

$this->faker->image(storage_path('img'),400,300, null, false)

InvalidArgumentException with message 'Cannot write to directory "/Users/paolo/Documents/code/famijam2/storage/img"'

kima's avatar

$this->faker->file is a call to a function that copies a file from a directory to another. it should be used as $this->faker->file($sourceDirectory = '/tmp', $targetDirectory = '/tmp', $fullPath = true)

from the code example, it looks like it's not what you are trying to accomplish

Please or to participate in this conversation.