【发布时间】:2017-08-30 00:19:25
【问题描述】:
UsersTapleSeeder.php:
<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory('App\User', 50)->create();
}
}
DatabaseSeeder.php:
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
protected $toTruncate = ['users'];
public function run()
{
foreach ($this-> $toTruncate as $table)
{
DB::table('users')->truncate();
}
$this->call(UsersTableSeeder::class);
}
}
ModelFactory.php:
<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'body' => $faker->sentences(),
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
];
});
我的用户表:
<?php
use Illuminate\Support\Facades\Schema;
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->text('body');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
我尝试了迁移、迁移回滚、composer dump-autoload。 在我添加正文列之前它一直有效,之后即使我删除它。
【问题讨论】:
-
你能提供完整的错误信息吗?
-
你能分享你的
User模型吗? -
这是完整的错误:[Symfony\Component\Debug\Exception\FatalErrorException] 无法访问空属性
-
User.php use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'body', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; }