Reputation: 7175
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();
}
Upvotes: 1
Views: 174
Reputation: 5609
public function statusUpdate(Request $request)
{
$id = $request->id; //get the id
$request['status_id'] = $request->status; //get the status value on status_id
DB::table('travel_request')
->where('id',$id )
->update(only(['status_id'])); //update only status_id field
return response()->json();
}
Upvotes: 0
Reputation: 163748
Using Eloquent:
public function statusUpdate(Request $request)
{
$status_id = Status::where('status', $request->status)->first()->id;
TravelRequest::where('id', $request->id)->update(compact('status_id'));
return response()->json();
}
Another way to do that is to keep statuses in config file, for example:
status => [
'waiting' => 1,
'approved' => 2,
],
Then code would look like this:
public function statusUpdate(Request $request)
{
TravelRequest::where('id', $request->id)
->update(['status_id' => config('conf.status')[$request->status]]);
return response()->json();
}
Upvotes: 1
Reputation: 3157
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();
}
Upvotes: 1