0

I am new to Laravel framework.

I am facing an issue, with migration of User table.I added nullable to 'firstname' of my table, which works but displaying the same error for 'lastname'column. I have included nullable() to every column, which is not the right way to do it. How to solve this?? Can anyone please suggest.

User migration table, facing issue with the 'firstname' column. create_user_table.php

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('firstname');
        $table->string('lastname');
        $table->string('email')->unique();
        $table->date('dob');
        $table->char('address',100);
        $table->bigInteger('phonenumber');
    });
}

UserController with store UserController.php

    public function store(Request $request)
        {
            $request->validate([
                'firstname'=>'required',
                'lastname'=>'required',
                'email'=>'required',
            ]);

            $user=new User([
                $user->firstname=>$request->get('firstname'),
                'lastname'=>$request->get('lastname'),
                'email'=>$request->get('email'),
                'dob'=>$request->get('dob'),
                'address'=>$request->get('address'),
                'phonenumber'=>$request->get('phonenumber')
                ]);
                $user->save();
                return redirect('/users')->with('success','user added');
        }
aynber
  • 22,380
  • 8
  • 50
  • 63
  • I do not see nullable as a default on the first name or last name column. nullable is correct, or default it to an empty string. Additionally, if `$request->get('firstname')` is null (no first name is passed in), then null is what will be sent to the database. You can change the default on that to an empty string with `$request->get('firstname', '')` – aynber Jun 09 '20 at 19:35
  • I haven't added nullable in the code here, but when I added to 'firstname' it gave me error for 'lastname'. I am passing the values to the table through form. In that case, should I include nullable to every column?? – Greeshma Nimmala Jun 09 '20 at 19:38
  • Correct, add nullable to any column that can have an empty value. – aynber Jun 09 '20 at 19:38
  • Also, `$user->firstname=>$request->get('firstname'),` should not have `$user` as the array key – aynber Jun 09 '20 at 19:39
  • If I include nullable, for columns and when I enter data through form for these fields 'null' values are displayed in the table. – Greeshma Nimmala Jun 09 '20 at 19:48
  • are you sure you have (firstname,lastname) in your $fillable in user model? – OMR Jun 09 '20 at 19:51
  • No, I have my id and email. Do I need to include all the columns in $fillable?? – Greeshma Nimmala Jun 09 '20 at 19:54
  • It's working now, I have added all the columns. Thank you @OMR – Greeshma Nimmala Jun 09 '20 at 19:59
  • Thank you :) @aynber – Greeshma Nimmala Jun 09 '20 at 20:00

2 Answers2

1

The easiest way to achieve this is:

   public function store(Request $request)
    {
        $request->validate([
            'firstname'=>'required',
            'lastname'=>'required',
            'email'=>'required',
        ]);

            $user=new User();
            $user->firstname = $request->firstname;
            $user->lastname = $request->lastname;
            $user->email = $request->email;
            $user->dob = $request->dob;
            $user->address = $request->address;
            $user->phonenumber = $request->phonenumber;
            $user->save();
            return redirect('/users')->with('success','user added');
    }

Also in your model, you have to add this line for mass assignment

protected $fillable = ['firstname','lastname','email','dob','address','phonenumber'];

You may check out this link for mass assignment: https://laravel.com/docs/7.x/eloquent#mass-assignment

Humza Faqi
  • 201
  • 2
  • 11
0

There is an errors in the code you posted, Here is the fix:

public function store(Request $request)
        {
            $request->validate([
                'firstname'=>'required',
                'lastname'=>'required',
                'email'=>'required',
            ]);

            $user=new User([
                'firstname' => $request->get('firstname'), // $user->firstname=>$request->get('firstname'),
                'lastname'=> $request->get('lastname'),
                'email' => $request->get('email'),
                'dob' => $request->get('dob'),
                'address' => $request->get('address'),
                'phonenumber' => $request->get('phonenumber')
                ]);
                $user->save();
                return redirect('/users')->with('success','user added');
        }

also here you are using mass assignment so you should add all used columns to your fillable array in your User model.

Here is a link on what is mass assignment

George
  • 716
  • 1
  • 12
  • 29