【问题标题】:Laravel faker valid() should not be called staticlly, but it is notLaravel faker valid() 不应该被静态调用,但它不是
【发布时间】:2021-05-31 00:07:48
【问题描述】:

我正在尝试使用 Laravel 中的 faker 提供的 valid() 方法,如下所示:

<?php

namespace Database\Factories;

use App\Models\Attribute;
use App\Faker\AttributeValue as AttributeValueProvider;
use App\Models\Supplier;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\AttributeValue;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;


class AttributeValueFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = AttributeValue::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition(): array
    {
        $this->faker->addProvider(AttributeValueProvider::class);
        $attribute = Attribute::orderBy('id', 'desc')->first();
        $attribute_name = $attribute->translate('en')->name;
        $attribute_value = $this->faker->valid($this->is_valid_attribute_value($this, $attribute->id))->get_attribute_values($attribute_name);
        $supplier_id = Supplier::max('id');
        return [
            'en' => $attribute_value['en'] + ['supplier_id' => $supplier_id],
            'ar' => $attribute_value['ar'] + ['supplier_id' => $supplier_id],
        ];
    }

    public function is_valid_attribute_value($attribute_value, $attribute_id): bool
    {
        $valid = Validator::make(['attribute_value_translations' => $attribute_value], [
            'attribute_value' => [Rule::unique('attribute_value_translations', 'value')->where(function($q) use ($attribute_id){
                $q->where('locale', 'en')
                    ->where('attribute_id', $attribute_id);
            })]
        ])->passes();
        return $valid;
    }
}



但它会引发以下错误:

call_user_func_array(): 参数 #1 ($callback) 必须是有效的 回调,非静态方法 App\Faker\AttributeValue::valid() 不能 被静态调用

如你所见,我不是静态调用它,请帮助

【问题讨论】:

  • 您实际上并没有向它传递函数回调,而是将函数的 result 传递给它。
  • 正如我从doc 看到的,valid() 除了function 作为参数,而不是boolean

标签: laravel laravel-8 faker


【解决方案1】:

如文档示例代码(以下摘录)中所述,Faker 需要一个回调作为 valid() 方法的参数。

$evenValidator = function($digit) {
    return $digit % 2 === 0;
};
for ($i = 0; $i < 10; $i++) {
    $values []= $faker->valid($evenValidator)->randomDigit;
}

但您的代码将函数的 result 传递给 valid() 方法,而不是函数本身。

$attribute_value = $this->faker
    ->valid($this->is_valid_attribute_value($this, $attribute->id))
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  this is a boolean value, not a callback
    ->get_attribute_values($attribute_name);

要使用您班级的验证功能,您需要将其更改为类似

$attribute_value = $this->faker
    ->valid([$this, 'is_valid_attribute_value'])
    ->yourFakeProvidedValue;

后跟提供的假值。 Faker 然后将提供的值传递给回调以检查它是否有效。

【讨论】:

  • 有效方法抛出 TypeError: Argument 1 passed to Faker\Generator::valid() must be an instance of Closure or null, array given, 如何解决?
  • 给出的示例是传递$this 的成员函数,并在此处使用数组语法。您也应该可以使用fn () =&gt; $this-&gt;is_valid_attribute_value($this, $attribute-&gt;id) 调用它。
猜你喜欢
  • 2015-04-18
  • 2015-05-28
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多