Laravel 5.2用户表播种器错误

时间:2016-04-16 19:43:48

标签: php mysql laravel laravel-5.2 artisan

我使用迁移创建了一个用户表,但无法为此表设置种子。当我从命令行运行 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();
    }

}

我的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

4 个答案:

答案 0 :(得分:0)

将此添加到用户模型,让用户模型处理密码bcrypt并存储。这样就可以解决了。

UserTableSeeder

example.com/something.aspx?id=1

user.php的

<script>
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://example.com/login", {username: 'bob', password: '123'}, true);
xhttp.send();
$.getJSON('http://www.example.com/something.aspx?id=1', function(data) {
console.log(data);
});
</script>

答案 1 :(得分:0)

您提供的代码没有错:)

你能告诉我们更多细节吗?或者你有没有试过

composer dump-autoloadphp artisan migrate:refresh --seed

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

试试这个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;');
    }
}