【问题标题】:When I run: php artisan db:seed. I get his output: Cannot access empty property当我运行时:php artisan db:seed。我得到他的输出:无法访问空属性
【发布时间】: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', ]; }

标签: php laravel


【解决方案1】:

$this-&gt; $toTruncate 在您的播种机中导致该错误。改成:

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    protected $toTruncate = ['users'];

    public function run()
    {
        foreach ($this->toTruncate as $table)
        {
            DB::table($table)->truncate();
        }


        $this->call(UsersTableSeeder::class);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-05
    • 2015-05-20
    • 2020-05-28
    • 2012-10-16
    • 2023-03-13
    • 1970-01-01
    • 2021-04-13
    • 2014-03-06
    相关资源
    最近更新 更多