0

I'm trying to generate a fake image to populate seeder data.

I created a seeder with this:

$this->faker->image(storage_path('app/public/products'), 500, 500)

When I try to access them through Laravel Nova to see them, I get this kind of URL: http://localhost/storage/var/www/html/storage/app/public/products/c8831002794cc55fe046c5e2b65794d4.png

In the DB it's saved as: /var/www/html/storage/app/public/products/c8831002794cc55fe046c5e2b65794d4.png

Any idea what I've done wrong?

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • Does this answer your question? [I want to add in database table just name of image, NOT a whole path of image and I want to do with Factory and Seeds](https://stackoverflow.com/questions/56292415/i-want-to-add-in-database-table-just-name-of-image-not-a-whole-path-of-image-an) – miken32 Feb 19 '23 at 02:11

2 Answers2

1

See the method signature for image():

function image(
    ?string $dir = null,
    int $width = 640,
    int $height = 480,
    ?string $category = null,
    bool $fullPath = true,
    bool $randomize = true,
    ?string $word = null,
    bool $gray = false,
    string $format = 'png'
)

Setting $fullPath to false will fix your issue:

$this->faker->image(storage_path('app/public/products'), 500, 500, null, false)
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • Now it's returning http://localhost/storage/3bf64f4fc195f024288ea2cf282513e5.png And in the database there's only the filename 3bf64f4fc195f024288ea2cf282513e5.png Maybe I'm missing something in Nova... i'll go re-read this doc. – Jonathan Lafleur Feb 18 '23 at 22:28
  • So yeah, my code follow the doc, the problem is that in the database it's not storing the path "after" the disk path, for instance here the products/ part should be in the db. I can prepend it to the faker results, but is there a more "standard" way of doing it ? – Jonathan Lafleur Feb 18 '23 at 22:36
0

Try this:

$this->faker->image('app/public/products',400,300, null, false) 

if you have a view then:

<img src="/my/path/{{ $item->image}}" >
jmvcollaborator
  • 2,141
  • 1
  • 6
  • 17