gpt4 book ai didi

php - Laravel,调用未定义的函数 Database\Seeders\factory()

转载 作者:行者123 更新时间:2023-12-03 11:21:05 32 4
gpt4 key购买 nike

运行命令时出现标题错误:

php artisan db:seed
我的截图:
enter image description here
我不知道这个问题从何而来。我正在寻找代码示例和解决方案,但我什么也没找到:(
文章TableSeeder.php
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
// use Laracasts\TestDummy\Factory as TestDummy;

class ArticlesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\Models\Article::class, 30)->create();
}
}
文章工厂.php
<?php

namespace Database\Factories;

use App\Models\Model;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class ModelFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = App\Models\Article::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'title' => $faker->text(50),
'body' => $faker->text(200)
];
}
}
数据库浏览器.php
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(ArticlesTableSeeder::class);
}
}
预先感谢您的帮助!

最佳答案

在 laravel 8 中删除了默认路由命名空间。
尝试改变:
文章TableSeeder.php:

 factory(App\Models\Article::class, 30)->create();
到:
\App\Models\Article::factory()->count(30)->create(); 
ArticleFactory.php:
protected $model = App\Models\Article::class;
到:
protected $model = \App\Models\Article::class;
你可能不得不改变:
 'title' => $faker->text(50),
'body' => $faker->text(200)
到:
 'title' => $this->faker->text(50),
'body' => $this->faker->text(200)

关于php - Laravel,调用未定义的函数 Database\Seeders\factory(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63816395/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com