简体   繁体   中英

Laravel Eloquent Model pass only certain parameters

On my Users model, can it only accept certain columns?

Like if I pass username, email and password, on the Users model, it will filter to only accept username and password only.

I tried to fill up the 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. Here is part of the migration file that ships with Laravel (found in 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. ( 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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