2

Hi my database structure is as follows:

Table: analytics

id | user_id(fk) | date | finalized(boolean)

Now I want to update the data in bulk. For example I want to select the data of user_id = 1 and date BETWEEN 2016-03-01 and 2016-03-20. And then I want to update the column finalized from 0 to 1 for all those records which I get in result.

What I had done is as follows:

 Analytics::where('user_id', $request->get('user_id'))
->whereBetween('date', [$request->get('from_date'), $request->get('to_date')])
->update(array('finalized' => 1));

But this is not working and not showing any error either. Please let know how can I do with the help of eloquent. Thanks in advance.

I have seen other questions in Stackover Flow too, but the answer suggests to run foreach loop, which I don't want. So is there any way, then kindly suggest me. Thanks again

Siddharth
  • 1,649
  • 4
  • 22
  • 47
  • Possible duplicate of [Eloquent model mass update](http://stackoverflow.com/questions/22430716/eloquent-model-mass-update) – cwang Mar 15 '16 at 06:35
  • @crystalwill, I have seen that question too, but did not find what I actually wanted. – Siddharth Mar 15 '16 at 06:37

1 Answers1

4

Are you sure that the $fillable property is set on the Analytics model, and it includes finalized? Also, have you confirmed that the request object has the dates and user ID you expect it to have?

Alternatively, you can go without Eloquent and hit the database directly.

DB::table('analytics')
    ->where('user_id', $request->user_id)->
    ->whereBetween('date', [$request->from_date, $request->to_date])
    ->update(['finalized' => 1]);
Dwight
  • 12,120
  • 6
  • 51
  • 64