I have reread a few times and have no idea what you are trying to achieve - can you explain?
Apr 12, 2022
2
Level 1
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 !
Please or to participate in this conversation.