0

I have a situation to update the row based on id and return the complete row. I can do it in two queries but i want to do it in one query only. I have write this...

 $result= DB::table($this->table)
          ->whereRaw($where['rawQuery'], $where['bindParams'] ? $where['bindParams'] : array())
          ->increment($updateField);
 if($result){
      return DB::table($updateTable)
             ->select('id')
             ->where('campaign_id',$where['bindParams'])
             ->where('date',date("Y-m-d"))
             ->get();
 }else{
     throw Exception("Error in fetching data");
 }
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70

2 Answers2

1

I copied this from what you commented in your question:

No i want to return the id of the updated row or complete row if possible

If you want to return the ID of the just updated 'row' you're talking about. You can use Eloquent to accomplish this.

You can use a route like this:

Route::put('demo/{id}', Controller@update);

Then in your update function in your controller you can get the ID. Find the model with $model = Model::find(id);

do things with the data you get. Like $model->name = Input::get('name');

Then use $model->save();

After saving you can do $model->id;

Now you get back the ID about the row you just updated.

Refer back to this question:

Laravel, get last insert id using Eloquent

Community
  • 1
  • 1
Dennis
  • 121
  • 4
-1

But any way it'll always be at least 2 queries (a SELECT and an UPDATE in MySQL, however you do it)

You can check Laravel Eloquent if you want a "cleaner" way to to this.

SimonDepelchin
  • 2,013
  • 22
  • 18