Search code examples
laravel-5factorylaravel-seeding

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


I need to create 5 record in database. I have name, price and path column. I want to add image for column path in database, which should contains name of image NOT whole path using just factory and seeds from Laravel.

 //PostFactory.php

 use App\Post;
 use Faker\Generator as Faker;

 $factory->define(Post::class, function (Faker $faker) {
 $filePath = public_path('images');



 return [
    'name' => $faker->name,
    'price' => $faker->numberBetween($min = 100, $max = 900),
    'path' => $faker->image($filePath,400,300),


        ];
 });

 //PostTableSeeder.php     

 use Illuminate\Database\Seeder;

 class PostTableSeeder extends Seeder
 {
   public function run()
  {
   factory(App\Post::class, 5)->create();
   }
 }

I expect to add just name of image, instead of that I only have whole path in database.


Solution

  • According to the Faker documentation, the signature of the method is:

    image($dir = null, $width = 640, $height = 480, $category = null, $fullPath = true, $randomize = true, $word = null)
    

    In your case, you could use 'path' => $faker->image($filePath,400,300,null,false) to get only the filename.