问题:使用工厂从 table 随机生成数据
Problem : randomly generate data from a table with a factory
我想从我的 garage
table 中获取一个 随机 ID,所以我遇到了问题。
<?php
namespace Database\Factories;
use App\Models\Car;
use App\Models\Garage;
use Illuminate\Database\Eloquent\Factories\Factory;
class CarFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Car::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->name(),
'release_year' => $this->faker->year(),
'garage_id' => Garage::inRandomOrder('id')->first('id'),
'created_at' => now()
];
}
}
问题是:
PHP Deprecated: Since fakerphp/faker 1.14: Accessing
property "name" is deprecated, use "name()" instead. in
/data/www-local/web/lara_sites/swan-formation-laravel/vendor/symfony/deprecation-contracts/function.php
on line 25 PHP Deprecated: Since fakerphp/faker
1.14: Accessing property "year" is deprecated, use "year()" instead. in
/data/www-local/web/lara_sites/swan-formation-laravel/vendor/symfony/deprecation-contracts/function.php
on line 25 Illuminate\Database\QueryException with message
'SQLSTATE[23000]: Integrity constraint violation: 1048 Column
'garage_id' cannot be null (SQL: insert into cars
(name
,
release_year
, garage_id
, created_at
, updated_at
) values (Alf
Bayer, 1994, ?, 2021-10-05 16:21:19, 2021-10-05 16:21:19))'
可以使用inRandomOrder方法对查询结果进行随机排序。
它没有参数,你可以像这样使用它:
'garage_id' => Garage::inRandomOrder()->first()->id,
你应该确保你的车库 table 不是空的。
你可以试试这个
'garage_id' => $this->faker->randomElement(Garage::query()->get('id')),
我想从我的 garage
table 中获取一个 随机 ID,所以我遇到了问题。
<?php
namespace Database\Factories;
use App\Models\Car;
use App\Models\Garage;
use Illuminate\Database\Eloquent\Factories\Factory;
class CarFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Car::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->name(),
'release_year' => $this->faker->year(),
'garage_id' => Garage::inRandomOrder('id')->first('id'),
'created_at' => now()
];
}
}
问题是:
PHP Deprecated: Since fakerphp/faker 1.14: Accessing property "name" is deprecated, use "name()" instead. in /data/www-local/web/lara_sites/swan-formation-laravel/vendor/symfony/deprecation-contracts/function.php on line 25 PHP Deprecated: Since fakerphp/faker 1.14: Accessing property "year" is deprecated, use "year()" instead. in /data/www-local/web/lara_sites/swan-formation-laravel/vendor/symfony/deprecation-contracts/function.php on line 25 Illuminate\Database\QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'garage_id' cannot be null (SQL: insert into
cars
(name
,release_year
,garage_id
,created_at
,updated_at
) values (Alf Bayer, 1994, ?, 2021-10-05 16:21:19, 2021-10-05 16:21:19))'
可以使用inRandomOrder方法对查询结果进行随机排序。 它没有参数,你可以像这样使用它:
'garage_id' => Garage::inRandomOrder()->first()->id,
你应该确保你的车库 table 不是空的。
你可以试试这个
'garage_id' => $this->faker->randomElement(Garage::query()->get('id')),