Home > Blockchain >  laravel requiring name in fillable array while it's in it
laravel requiring name in fillable array while it's in it

Time:01-15

my problem is when I register a new user, after I submit the post request it throws an exception saying Add [name] to fillable property to allow mass assignment on [Illuminate\Foundation\Auth\User] and its already exist in fillable.

namespace App\Http\Controllers\Auth;

/*imports here*/

class SignupController extends Controller
{
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'surname' => 'required',
            'username' => 'required|min:3|max:20',
            'phone' => 'required',
            'email' => 'required',
            'password' => 'required|confirmed',
        ]);

        User::create([
            'name'=> $request->name,
            'surname'=> $request->surname,
            'username'=> $request->username,
            'phone'=> $request->phone,
            'email'=> $request->email,
            'password'=> Hash::make($request->password),
        ]);
    }
}

and when I execute php artisan migrate, it throws

Migrating: 2022_01_14_124437_add_username_to_users_table

   Error

  Call to undefined method Illuminate\Database\Schema\MySqlBuilder::dropColumnIfExists()

  at C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:261
    257▕         if (! $instance) {
    258▕             throw new RuntimeException('A facade root has not been set.');
    259▕         }
    260▕
  ➜ 261▕         return $instance->$method(...$args);
    262▕     }
    263▕ }
    264▕

  1   C:\Users\xxgam\OneDrive\Desktop\projects\deneme\database\migrations\2022_01_14_124437_add_username_to_users_table.php:16
      Illuminate\Support\Facades\Facade::__callStatic("dropColumnIfExists")

  2   C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:394
      AddUsernameToUsersTable::up()

C:\Users\xxgam\OneDrive\Desktop\projects\deneme>php artisan migrate:rollback
Rolling back: 2019_12_14_000001_create_personal_access_tokens_table
Rolled back:  2019_12_14_000001_create_personal_access_tokens_table (6.35ms)

C:\Users\xxgam\OneDrive\Desktop\projects\deneme>php artisan migrate
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated:  2019_12_14_000001_create_personal_access_tokens_table (264.26ms)
Migrating: 2022_01_14_124437_add_username_to_users_table
Migrated:  2022_01_14_124437_add_username_to_users_table (115.22ms)

User Class

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $fillable = [
        'name',
        'surname',
        'username',
        'phone',
        'email',
        'password',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

I only know the basics in laravel, so if I missed something to write here tell me and I'll write it.

CodePudding user response:

I guess you've deleted some code from your migration file by accident.

you can compare this code with the one you have.

check the use's and the

extends Migration

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateFailedJobsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('<your_table_name>', function (Blueprint $table) {
            \\your columns
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('failed_jobs');
    }
}

CodePudding user response:

It would appear you are referencing the wrong Model. Your User model is App\Models\User but you are referencing Illuminate\Foundation\Auth\User. Adjust your alias to use the correct User model in your Controller:

use App\Models\User;
  •  Tags:  
  • Related