简体   繁体   English

Laravel雄辩模型仅传递某些参数

[英]Laravel Eloquent Model pass only certain parameters

On my Users model, can it only accept certain columns? 在我的Users模型上,它只能接受某些列吗?

Like if I pass username, email and password, on the Users model, it will filter to only accept username and password only. 就像我在“ Users模型上传递用户名,电子邮件和密码一样,它将进行过滤以仅接受用户名和密码。

I tried to fill up the protected $fillable = ['username','password']; 我试图填写protected $fillable = ['username','password']; but it seems not to work. 但似乎不起作用。

That depends on the schema you have for the User model in your database. 这取决于数据库中用户模型的架构。 There is a migration file for the users table included with laravel, and this should be in the database if you have run php artisan migrate. laravel随附有一张用于users表的迁移文件,如果您已运行php artisan migration,则该文件应该在数据库中。 Here is part of the migration file that ships with Laravel (found in database/migrations/): 这是Laravel随附的迁移文件的一部分(位于database / migrations /中):

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

You can see that there are only three columns you can fill by default, and you could add more here if you wanted. 您会看到默认情况下只能填充三列,并且您可以根据需要在此处添加更多列。 Although you would need to run php artisan migrate:rollback before making the changes to the migration file and then run php artisan migrate again. 尽管您需要在更改迁移文件之前运行php artisan migrate:rollback,然后再次运行php artisan migration。 ( https://laravel.com/docs/5.4/migrations ) https://laravel.com/docs/5.4/migrations

The protected $fillable array just chooses which properties can be mass-assigned ( https://laravel.com/docs/5.4/eloquent#mass-assignment ) so you will need these if you want to use the static create method like this: 受保护的$ fillable数组仅选择可以批量分配的属性( https://laravel.com/docs/5.4/eloquent#mass-assignment ),因此,如果您想使用静态创建方法,则将需要这些属性:

User::create(['name' => 'John Doe', 'email' => 'john@doe.com, 'password' => bcrypt('password')]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM