Ayman Tarig
Ayman Tarig

Reputation: 221

cannot send post request in Laravel

I am trying to send a post request and it give me this error. I think the error is caused by something in the controller but I do not know what it is. I've provided all the related codes. Can anyone help me to fix the error ?

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'first_name' cannot be null (SQL: insert into users (first_name, last_name, email, phone_number, password, updated_at, created_at) values (?, ?, ?, ?, ?, 2020-02-26 07:54:56, 2020-02-26 07:54:56))

Controller:

public function store(Request $request)
    {

        $user = new User();

        $user->first_name = $request->first_name;
        $user->last_name = $request->last_name;
        $user->email = $request->email;
        $user->phone_number = $request->phone_number;
        $user->password = $request->password;

        $user->save();

        return new UserResource($user);
    }

Model:

class User extends Model
{
    protected $guarded = [];
}

UserResource:

public function toArray($request)
    {
        // return parent::toArray($request);

        return [
            'id' => $this->id,
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'email' => $this->email,
            'phone_number' => $this->phone_number,
            'password' => $this->password
        ];
    }
}

migration:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email');
            $table->string('phone_number');
            $table->string('password');
            $table->timestamps();
        });
    }

Request body:

{
    'first_name': 'ayman',
    'last_name': 'tarig',
    'email': '[email protected]',
    'phone_number': '010010100',
    'password': '1234'
}

Upvotes: 2

Views: 1209

Answers (3)

Poldo
Poldo

Reputation: 1932

Make sure you import Illuminate\Http\Request

use Illuminate\Http\Request;

Then using postman try to invoke your post method.

Upvotes: 0

Mahmud hasan
Mahmud hasan

Reputation: 1046

if you making this request through a html form, make sure that the name of each input you spelled correctly. And if you using postman then make sure that you have sent a post request, because all thing are looking good.

Upvotes: 0

loic.lopez
loic.lopez

Reputation: 2103

First I think you may use Mass Assignment from the Eloquent ORM,

From the Laravel Eloquent ORM Documentation

You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.

In order to make your field updated you need to use the fillable array of an Eloquent model:

class User extends Model
{
    protected $guarded = [];

    protected $fillable = [
            'id',
            'first_name',
            'last_name',
            'email',
            'phone_number',
            'password',
     ];
}

You way want to use a custom request in order to verify in provided data is in correct format and not null: laravel.com/docs/master/validation#form-request-validation

For more complex validation scenarios, you may wish to create a "form request". Form requests are custom request classes that contain validation logic.

As follows:

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'first_name' => 'required',
        'last_name' => 'required',
        'email' => 'required|email',
        'phone_number' => 'required',
        'password' => 'required'
    ];
}

Upvotes: 1

Related Questions