Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Syrine123's avatar

How To Update field in database table using Foreign Key?

Hello evryone, Actually I' m working on reactjs project with laravel API. I have table named status and another named complain. the STATUS table has a forign key from complain Table . I want to create a put API but with condition like this eg: if primary_key==foreign key then update values in mother table. This is my Attempt without any condition:

        public function updateStatus(Request $request, $compl_id){
            $data = $request->validate([
                'compl_job_status' => ''
            ]);
            $input = $request->all();
            $compl = complain::findOrFail($compl_id);
            $compl->update($input);
            if($compl->save()){       
              return response()->json([
                'message' => ' Updated Successfully !',
                'status' => 200,
            ]);}
            return $compl;

        }

PS: the foreign key in the other table is named complain id this is the mother table :

     Schema::create('complains', function (Blueprint $table) {
            $table->increments('compl_id');
            $table->unsignedInteger('building_id')->nullable();
            $table->foreign('building_id')->references('building_id')->on('buildings')->onDelete('set null');
            $table->unsignedInteger('user_id')->nullable();
            $table->foreign('user_id')->references('user_id')->on('users')->onDelete('set null');
            $table->string('compl_title',200);
            $table->string('compl_description',1000);
            $table->date('compl_date');
            $table->tinyInteger('compl_job_status');
            $table->string('compl_assigned_to',100);
            $table->string('compl_solution',500);
            $table->string('compl_complainBy',100);
            $table->string('compl_name',250);
            $table->json('compl_pictures');
            $table->string('compl_email',100);
            $table->string('compl_phone',50);
            $table->timestamps();
        });
    }

this is the othe table:

 Schema::create('statuses', function (Blueprint $table) {
            $table->increments('status_id');
            $table->unsignedInteger('complain_id')->nullable();
            $table->foreign('complain_id')->references('compl_id')->on('complains')->onDelete('set null');
            $table->unsignedInteger('user_id')->nullable();
            $table->foreign('user_id')->references('user_id')->on('users')->onDelete('set null');
            $table->tinyInteger('compl_status');

            $table->timestamps();
        });

Any suggestion Please !

0 likes
2 replies
tykus's avatar

I have reread a few times and have no idea what you are trying to achieve - can you explain?

Syrine123's avatar

@tykus Actually I'm working on notifications. The admin add a complain and assigned it to an employee with a default status :'Pending' Then the employee recieve a notification and change the actual status But I stored the status in another table beacause the one who can accept and make changes in the table comlplain is the Admin. And then After changing the status of the complain by the employee the admin can accept. when he accepts the status of the complain should take the value of the status in the table statuses. What I want is to make a function that can update the value of the status in table complains with the one in the table statuses. I hope you understand me

Please or to participate in this conversation.