Laravel 5.2用户表种子程序错误


Laravel 5.2 User table seeder error

我使用迁移创建了一个用户表,但无法为该表设置种子。当我从命令行运行php artisan db:seed时,我得到了以下错误

[Symfony'Component'Debug'Exception'FatalErrorException]
      parse error, expecting `"variable (T_VARIABLE)"'

我的UsersTableSeeder.php种子程序有以下行:

<?php
use Illuminate'Database'Seeder;
use App'User;
class UsersTableSeeder extends Seeder
{
    public function run()
    {
        $user = new User;
        $user->name = 'John Doe';
        $user->email = 'john@gmail.com';
        $user->password = bcrypt('password');        
        $user->save();
    }
}

My User.php型号:

<?php
namespace App;
use Illuminate'Foundation'Auth'User as Authenticatable;
class User extends Authenticatable
{
    protected $fillable = [
        'name', 'email', 'password',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
}

我该怎么解决这个问题?TIA

将其添加到用户模型中,并让用户模型处理密码bcrypt和存储。这样就可以解决问题。

UserTableSeeder

public function run()
    {
        $user = new User;
        $user->name = 'John Doe';
        $user->email = 'john@gmail.com';
        $user->password = 'password';        
        $user->save();
    }

用户.php

Public function setPasswordAttribute($password) 
{ 
    return $this->attributes['password'] = bcrypt($password); 
}

您提供的代码没有错:)

你能给我们更多的细节吗?或者你试过吗

CCD_ 1和CCD_ 2

如文档所述,尝试直接插入数据库:

https://laravel.com/docs/5.2/seeding

public function run()
{
    DB::table('users')->insert([
        'name' => 'John Doe',
        'email' => 'john@gmail.com',
        'password' => bcrypt('password'),
    ]);
}

只需尝试此UserTableSeeder

public function run()
    {
        User::truncate();
        $pavan = User::create([
            'name' => 'something',
            'email' => 'something@gmail.com',
            'password' => bcrypt('password'),
            'status' => 1,
            'theme' => 'green',
            'is_delete' => 0
        ]);
}

在DatabaseSeeder.php 中

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();
        'DB::statement('SET FOREIGN_KEY_CHECKS = 0');
         $this->call(UserTableSeeder::class);
        'DB::statement('SET FOREIGN_KEY_CHECKS=1;');
    }
}