0

Screenshot of the error page

enter image description here

How do i go about this please? I created another table in my db where i generate OTPs and send to the users. in my table i made user_id and id(primary) but due to the nature of my code and how i want the site to function, i set user_id as a reference of id so that whenever OTP is generated, it will be the current user's ID

It seems there's no default value for user_id so i did ;

UserOTP::create(["user_id"=> auth()->id()]); 

but its not working, what is wrong with my code please?

    <?php

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

class CreateUserOtpsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {


        Schema::create('user_otps', function (Blueprint $table) {

            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->integer('code');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();

        });


    }

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

This is the db migration file

Rakesh kumar Oad
  • 1,332
  • 1
  • 15
  • 24
  • Would you mind sharing the `UserOTP.php` model file please? – Huy Phạm Feb 08 '22 at 10:32
  • I still get the same error after doing $table->increments('id');. There are two fields in the table , Id is different from user_id but i want to set user_id to id when user submit an OTP request , so that i will be able to verify the current user and later drop the data. Dont know if you get what im trying to explain – Rossenburg Feb 08 '22 at 10:34
  • @HuyPhạm UserOTP model **bold** `created_at->diffInMinutes(now()) > 5; } public static function generateCode() { return random_int(100000,999999); } }` – Rossenburg Feb 08 '22 at 10:35
  • 1
    Does this answer your question? [General error: 1364 Field 'user\_id' doesn't have a default value](https://stackoverflow.com/questions/43651446/general-error-1364-field-user-id-doesnt-have-a-default-value) – Meher Ullah Khan Raj Feb 08 '22 at 10:46
  • Please post your controller code or wherever you're creating the full `UserOtp`. – BadHorsie Feb 08 '22 at 10:57
  • @BadHorsie i found the problem and resolved it. Thank You – Rossenburg Feb 08 '22 at 11:54
  • @Rossenburg What was the problem, so others will know in future? – BadHorsie Feb 08 '22 at 19:21

3 Answers3

0

It's a SQL error not a PHP one. You're trying to insert a new row without a user_id but there's no default value set for that column, so it doesn't know what to set it as.

In your migration class, try giving it a default value:

$table->unsignedBigInteger('user_id')->default(null);

In any case, it also seems like you're not inserting with a user_id, only code, so you need to probably ensure that field can be persisted.

Edit: Yes, now you've posted your model, the problem is you haven't allowed the user_id field to be fillable:

// UserOtp.php
protected $fillable = ['code' , 'user_id'];
BadHorsie
  • 14,135
  • 30
  • 117
  • 191
  • Setting a null value works but i dont want it to be null, instead i want to set the value as the primary id value so that i can verify a specific user whole registered on the site and later drop the data. if i set a null value i wont be able to verify the user phone number without knowing the specific user – Rossenburg Feb 08 '22 at 10:47
  • @Rossenburg Yes I understand. If `user_id` is being inserted as null, either the `id` itself isn't being returned properly for some reason and has a value of null, or the `user_id` field is not being set on the model, or the `user_id` field isn't allowed to be saved. Have you tried debugging everything properly to see the values of the model before you save? – BadHorsie Feb 08 '22 at 11:01
0

It's because you haven't set user_id field in $fillable property. Hence, Laravel ignored provided value for this field when you are using create() method.

However, before using the create method, you will need to specify either a fillable or guarded property on your model class. These properties are required because all Eloquent models are protected against mass assignment vulnerabilities by default.

Related document

 protected $fillable = ['code' , 'user_id'];
Huy Phạm
  • 888
  • 9
  • 24
0

Okay so i found it. Instead of keeping the answer to myself, i wanna share to whoever that may have this issue in the future.

so i made a function called rules where i forgot to add user_opts to code .

 public function rules()
{
    return [
        'otpcode' => ['required','numeric',Rule::exists('user_otps','code')->where(function($query){
            $query->whereUserId(auth()->id());
        })]
    ];

adding user_otps to code actually solved it and everything is working now. The main problem wasnt this but rather, as the previous answers were saying. My user_id field wasnt receiving a value on submit.

Happy to resolve this issue and thanks to whoever helped