所以我在迁移数据库之后尝试了一个基本的php artisan db:seed
,但它一直在cmd中返回标题错误 - [Symfony\Component\Debug\Exception\FatalErrorException] Class 'User' not found
我尝试过的事情
db:seed
函数--seed
语法以下是迁移
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
我相信这里的一切都是正确的,现在这里是用户类。
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
}
现在最后是所有重要的数据库播种机
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
// $this->call('UserTableSeeder');
$this->call('UserTableSeeder');
Model::reguard();
}
}
class UserTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->delete();
User::create(['email' => 'John@doe.com']);
}
}
这就是我的完整语法,如果需要更多文件,请请求他们,我会更新我的问题。
答案 0 :(得分:20)
在根命名空间的DatabaseSeeder
中,您可以调用类User
。因此尝试加载类User
。但是,类User
的定义位于命名空间App
中。您应该在App\User
中使用DatabaseSeeder
或在文件顶部添加use App\User;
DatabaseSeeder
<?php
use App\User;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
// $this->call('UserTableSeeder');
$this->call('UserTableSeeder');
Model::reguard();
}
}
class UserTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->delete();
User::create(['email' => 'John@doe.com']);
}
}
聚苯乙烯。假设您的类App \ User可以自动加载,如果您没有这样的类只需导入Illuminate one
use Illuminate\Foundation\Auth\User;
另一方面,我发现调试工匠输出非常有用。您应该使用标志-vvv
,它会为输出消息添加极端冗长,包括完整的堆栈跟踪。
php artisan migrate -vvv