3

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!

mstdmstd
  • 2,195
  • 17
  • 63
  • 140

1 Answers1

8

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

Daniel Mesa
  • 586
  • 3
  • 12
  • Your examples are not clear . What is "creatory_id" ? I will detalize: With ads table I also have categories table with id, name fields. I need to add dummy data into ad_categories (fields ad_id, category_id), WHERE ad_id - REF TO NEWLY CREATED ad, category_id - ref to one of existing category. Thanks! – mstdmstd May 27 '21 at 05:05
  • 1
    Oh, I thought it was a one-to-many relationship. But you can relate an ad to a random dummy category – Daniel Mesa May 27 '21 at 05:08
  • Sorry, not clear. Coold you please give example? and I can not catch how can I get and use NewlyAd.id ? – mstdmstd May 27 '21 at 05:43
  • Thanks! I made it with some fixing your code : return $this->afterCreating(function (Ad $newAd) { // Ad model is returned here $adCategory = AdCategory::factory()->count(1)->create([ 'ad_id' => $newAd->id, 'category_id' => $this->faker->randomElement(Category::all())['id'], ]); }); Please edit it and I will ecept your answer! – mstdmstd May 27 '21 at 06:59
  • 1
    @mstdmstd Done! – Daniel Mesa May 27 '21 at 16:16