Search code examples
laravellaravel-factory

How with laravel factory add data to related table?


In laravel 8 app I add dummy data for ads table with factory:

$ads = Ad::factory()->count(10)->expired($year, $month)->create([
]);

And with database/factories/AdFactory.php:

<?php

namespace Database\Factories;

use Config;
use Carbon\Carbon;
use App\Models\Ad;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use \Cviebrock\EloquentSluggable\Services\SlugService;

class AdFactory extends Factory
{
    protected $model = Ad::class;

    public function definition()
    {
        $text= $this->faker->text;
        $slugValue = SlugService::createSlug(Ad::class, 'slug', $text);

        return [
            'title' => $text,
            'ad_token' => $text,
            'slug' => $slugValue,
            'phone_display' => (rand(1, 3) == 1),
            'has_locations' => (rand(1, 4) == 1),
            'status' => 'A',
            'price' => mt_rand(10, 500),
            'ad_type' => (rand(1, 2) == 1 ? 'B' : 'S'),
            'description' => $this->faker->paragraphs(rand(1, 4), true),
            'creator_id' => rand(1, 5),
        ];
    }

    public function expired($year, $month)
    {
        return $this->state(function (array $attributes) use($year, $month) {
            $dateStr= $year.'-'.str_pad($month, 2, "0", STR_PAD_LEFT).'-01';
            $startDate= Carbon::createFromFormat('Y-m-d', $dateStr);
            return [
                'expire_date' => $this->faker->dateTimeInInterval($startDate, '1 month', Config::get('app.timezone'))->format('Y-m-d H:i:s')
            ];
        });
    }
}

It works ok, but I wonder how can I add dummy data into related ad_categories( has ad_id field ) ? definition methods returns ad object(with new id) and where can I get access to it?

Thanks!


Solution

  • You can make an CategoryFactory class and create Dummy categories, then select a random category as category_id

    For one-to-many relationship

        //...
        public function definition()
        {
            return [
                //your code
                'creatory_id' => $this->faker->randomElement(Category::all())['id'],
            ];
        }
        //...
    

    or

        public function definition()
        {
            return [
                //your code
                'creatory_id' => factory(Category::class)->create()->id,
            ];
        }
    

    The second solution will create a new category for each Ad

    For many-to-many relationship

    namespace Database\Factories;
    
    use App\Models\AdFactory;
    use App\Models\Category;
    use Illuminate\Database\Eloquent\Factories\Factory;
    use Illuminate\Support\Str;
    
    class AdFactory extends Factory
    {
        /**
         * The name of the factory's corresponding model.
         *
         * @var string
         */
        protected $model = Ad::class;
    
        /**
         * Configure the model factory.
         *
         * @return $this
         */
        public function configure()
        {
            return $this->afterCreating(function (Ad $newAd) { 
                $adCategory = AdCategory::factory()->count(1)->create([ 'ad_id' => $newAd->id, 'category_id' => $this->faker->randomElement(Category::all())['id']); 
            }); 
        }
    
        // ...
    }
    

    For more information visit https://laravel.com/docs/8.x/database-testing#factory-callbacks

    and https://laravel.com/docs/8.x/database-testing#polymorphic-many-to-many-relationships