首页 > 解决方案 > 如何为 laravel 8 工厂设置参数?

问题描述

在我的 Laravel 8 中,当我在工厂调用中设置参数时,在https://laravel.com/docs/8.x/database-testing阅读工厂的工作原理:

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

我没有找到如何在我的工厂数据库/工厂/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);


        $startDate = new Carbon('first day of this month');
        return [
            'title' => $text,
            'ad_token' => $text,
            'slug' => $slugValue,
            'phone_display' => (rand(1, 3) == 1),
            'has_locations' => (rand(1, 4) == 1),
            'status' => 'A', // password
            'price' => mt_rand(10, 500),
            'ad_type' => (rand(1, 2) == 1 ? 'B' : 'S'),
            'expire_date' => $this->faker->dateTimeInInterval($startDate, '1 month', Config::get('app.timezone'))->format('Y-m-d H:i:s'),
            'description' => $this->faker->paragraphs(rand(1, 4), true),
            'creator_id' => rand(1, 5),
        ];
    }
}

在这种情况下,年和月不是广告模型的字段,但我需要它们为给定月份的 expire_date 设置值。

哪种方式有效?

修改块: 阅读https://laravel.com/docs/8.x/database-testing#factory-states 我重新制作了工厂,但我没有找到如何获取我的附加参数:

<?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\Sluggable;
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);

        \Log::info(  varDump($this, ' -1 $attributes::') );
        $startDate = new Carbon('first day of this month');
        return [
            'title' => $text,
            'ad_token' => $text,
            'slug' => $slugValue,
            'phone_display' => (rand(1, 3) == 1),
            'has_locations' => (rand(1, 4) == 1),
            'status' => 'A', // password
            'price' => mt_rand(10, 500),
            'ad_type' => (rand(1, 2) == 1 ? 'B' : 'S'),
            'expire_date' => $this->faker->dateTimeInInterval($startDate, '1 month', Config::get('app.timezone'))->format('Y-m-d H:i:s'),
//            'description' => $this->faker->paragraphs(rand(1, 4), true),
            'creator_id' => rand(1, 5),
        ];
    }

    public function expired()
    {
        return $this->state(function (array $attributes, $var1=null, $var2=null) {

            // $var1and $var 2 are null and $attributes has no year/month parameters 
            \Log::info(  varDump($var1, ' -1 expired $var1::') );
            \Log::info(  varDump($var2, ' -1 expired $var2::') );
            \Log::info(  varDump($attributes, ' -1 expired $attributes::') );
            return [ // This changes applied. Just example
                'description' => 'suspended expired',
            ];
        });
    }
}

我在工厂调用中传递参数:

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

?

谢谢!

标签: laravellaravel-factory

解决方案


推荐阅读