1

I have a problem with laravel when I try to update the record and add it's foreign key to NULL

It didn't update at all.

There is no error and I check the Mass Assignment too.

Migration file:

Schema::create('time_slots', function (Blueprint $table) {
   $table->id();
   $table->enum('duration', ['30', '60']);
   $table->double('price', 6, 2)->nullable();;
   $table->enum('currency', ['usd', 'egp'])->nullable();
   $table->dateTime('start_time');
   $table->dateTime('end_time');
   $table->enum('status', ['available', 'booked', 'finished'])->default('available');
   $table->enum('type', ['video', 'text', 'phone', 'not-set'])->default('not-set');
   $table->unsignedBigInteger('session_id');
   $table->foreign('session_id')->references('id')->on('sessions')->onDelete('cascade');
   $table->unsignedBigInteger('patient_id')->nullable();
   $table->foreign('patient_id')->references('id')->on('patients')->onDelete('cascade');
   $table->timestamps();
});

Update query

$timeslot->currency = null;
$timeslot->price = null;
$timeslot->patient_id = null;
$timeslot->status = 'available'; // that is the only column that update correctly
$timeslot->save();
2
  • 1
    Tested with Laravel 7.16.1 and it runs without any errors and the data changes made to update an existing Timeslot record also get persisted. I have set protected $guarded = []; on Timeslot model. MySQL Ver 8.0.19, PHP 7.4.12. Are you getting any errors - maybe in log file(s)? Pls check Commented Nov 22, 2020 at 20:16
  • it fixes but really i don't know what happens Commented Nov 23, 2020 at 1:02

1 Answer 1

2

Just replace currency definition like this

$table->enum('currency', [null,'usd', 'egp'])->nullable();

or this

$table->enum('currency', ['usd', 'egp', null])->nullable()->default(null)
Sign up to request clarification or add additional context in comments.

4 Comments

and what about the foreign key it's the most important one here
yeah im also looking it
Mine works perfectly.. Which laravel version are you trying
i use version 7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.