使用php artisan tinker,Job::factory()->count(10)->create();显示
Error Class "job" not found.
不知道是哪里出了问题。
我的Job.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Job extends Model
{
protected $table = 'job_listings';
protected $fillable = ['title', 'salary'];
}我的JobFactory.php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Job>
*/
class JobFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'title' => $this->faker->jobTitle(),
'salary' => $this->faker->numberBetween(30000, 100000),
];
}
}
Job.php
JobFactory.php
使用方式
进入 Tinker:
然后执行:
在 Tinker 中的使用方式
如果是在 Tinker 中操作,不能使用 use 语句,但可以直接这样写:
如果是在 Seeder、Controller 或 Artisan Command 中使用,就可以这样写: