user3386779 user3386779 - 1 month ago 8
MySQL Question

fetch value from table and update second table in same controller in laravel

I have two table named as 'travel_request' and 'status'.I have status value.based on that value I want to fetched the status table 'id' and want to update the "travel_request" table status_id in same controller.If I have the status value 'waiting' I want to fet the the id=1 and from status table and need to insert it in travel_request table

status table-id,status
travel_request-id,status_id

public function statusUpdate(Request $request)
{
$request_data = $request->all();
$id = $request_data['id'];
$status=$request_data['status']; //waiting(1),approved(2),
DB::table('travel_request')
->where('id',$id )
->update(['status_id' => $status]);
return response()->json();
}

Answer

Did you mean like this:

public function statusUpdate(Request $request)
{
  $request_data = $request->all();
   $id = $request_data['id'];   
   $status=$request_data['status'];  //waiting(1),approved(2), 
  DB::table('travel_request')
        ->where('id',$id )
        ->update(['status_id' => DB::table('status')->where('COLUMN_NAME', $status)->first()->id]); 
  return response()->json();        
}