Home > OS >  I am always getting an error everytime i try to add data in db: SQLSTATE[42703]: Undefined column: 7
I am always getting an error everytime i try to add data in db: SQLSTATE[42703]: Undefined column: 7

Time:11-18

So, theres already a pre-made table in the database. Now, I am trying to connect my project to that specific table in the db. but everytime i try to insert data, im getting this error

"SQLSTATE[42703]: Undefined column: 7 ERROR: column "updated_at" of relation "applicant_account_tbl" does not exist\nLINE 1: ...", "last_name", "birthdate", "email", "password", "updated_a...\n ^ (SQL: insert into "applicant_account_tbl" ("first_name", "middle_name", "last_name", "birthdate", "email", "password", "updated_at", "created_at") values (James, Middle, Harden, 1998-11-05, [email protected], $2y$10$CQRx2rmNrHahA6Lrw3OsIufeZXvAEhUOquotthM.9dr40NKRu1bJK, 2022-11-18 00:16:01, 2022-11-18 00:16:01))" trace : [,…]

Here is my model

class UserModel extends Model
{
    use HasFactory;

    protected $table = "applicant_account_tbl";

    protected $primarykey = "seq_id";

    public $incrementing = false;

    protected $fillable = [
        "first_name",
        "middle_name",
        "last_name",
        "birthdate",
        "email",
        "password",
    ];
}

Here is my controller for registration

public function registerUser(Request $request)
{
    $validator = Validator::make($request->all(), [
        "first_name" => "required",
        "middle_name" => "required",
        "last_name" => "required",
        "birthdate" => "required",
        "email" => "required|email|unique:user_models",
        "password" => "required|min:5|max:20",
    ]);

    if ($validator->fails()) {
        return response()->json([
            "status" => 400,
            "errors" => $validator->messages(),
        ]);
    } else {
        $user = new UserModel();
        $user->first_name = $request->input("first_name");
        $user->middle_name = $request->input("middle_name");
        $user->last_name = $request->input("last_name");
        $user->birthdate = $request->input("birthdate");
        $user->email = $request->input("email");
        $user->password = Hash::make($request->password);
        $user->save();

        return response()->json([
            "status" => 200,
            "message" => "You have registered successfuly",
            "first_name" => $user->first_name,
            "middle_name" => $user->middle_name,
            "last-name" => $user->last_name,
            "birthdate" => $user->birthdate,
            "email" => $user->email,
        ]);
    }
}

CodePudding user response:

add $timestamps = false to UserModel

/**
 * Indicates if the model should be timestamped.
 *
 * @var bool
 */
public $timestamps = false;

https://laravel.com/docs/9.x/eloquent#timestamps

  • Related