Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

luyznascimento's avatar

Eloquent Updates More Than Requested

Hi,

There s a very strange behavior of Eloquent. I have a model called - tickets. The action is just to update (Mass Assignment) using "whereIn"...

This is a sample:

Ticket::whereIn('reg', ["1900315150","1900318748","1900310306","1900320420","1900316811"])->update(['user' => 1, 'status' => 1]);

But when I query - right after that -, comes 10 results!

How come?

Thanks in advance everyone here that can give a hand on this.

Regards,

0 likes
27 replies
Nakov's avatar

And how many records do you get if you run this:

$result = Ticket::whereIn('reg', ["1900315150","1900318748","1900310306","1900320420","1900316811"])->count();

dd($result);

?

Nakov's avatar

Can you then show what do you query right after that, because to me all seems fine with your query above?

Check your database then if only the expected values are being updated, they are not a lot so you can check manually to make sure that it is the expected result.

You can also install PhpDebugbar or Telescope and see the Query that is being executed, inspect that. Or listen to the query events like this

https://laravel.com/docs/5.8/database#listening-for-query-events

and make sure that the underlying DB call is correct.

luyznascimento's avatar

Nakov,

The first query copy I have pasted above whas taken from "laravel-debugbar". And that´s the problem... I am completly confused whit this scenario, nothing seems to be wrong at all! :-)

Nakov's avatar

What I was saying is, that the 10 results part might be coming from another query that is being executed after the update.. so it depends on what you have on the page. That's why I mentioned that you check manually the DB, or manually listen for the query that is being executed, copy the RAW SQL, run it on your Database and make sure that it is being run only on those 5 records, and not on 10. That's all that I can think of.

luyznascimento's avatar

Nakov,

I just created an specific Controller for this action, just to remove any undesireble variable... so just responding your inquiries, everything (code) happens on Controller! I have a route (method / get) specifically for this that calls the Controller...

And more, I get, as you stated above, the query generated by Debugbar, paste it on my Workbench (Mysql) and run the query that executes normally the five items part of "whereIn"!

Nakov's avatar

@luyznascimento but when you run this:

Ticket::whereIn('reg', ["1900315150","1900318748","1900310306","1900320420","1900316811"])->update(['user' => 1, 'status' => 1]);

Are there really 10 records updated in the Database? Can you check that manually ?

luyznascimento's avatar

Nakov,

First of all, thanks for your patience.

#1 - I reset "reg" table; #2 - Run a query to check that there´s no status = 1; #3 - Run the page again. And then, I run the query (#2) again with "status=1" comes 10 results. The first ones (those part of whereIn) as expected, and more five results, just exactly the following ones.

Nakov's avatar

I don't mind spending time helping my friend :)

I tried similar query on my project, and it ran only on those rows that I expected it to run. So I really don't know what goes wrong in your case. If you can show the RAW query here that is being executed, I might be able to help a bit more, otherwise I hope that someone else will notice the thread here and help more if they get what I am missing too.

Can the reg column be null maybe that's an issue? I really don't know, just assumption here :)

luyznascimento's avatar

Nakov,

Thanks!

Well, no, "reg" it is not null.

And the lines of my first post here has everything.

Regards,

jlrdw's avatar

But when I query - right after that -, comes 10 results!

What is the query " right after that ".

Nakov's avatar

@luyznascimento so share the query that you are using below as that's obviously where the error is.. I asked you above as well

luyznascimento's avatar

@nakov and @jlrdw ,

Guys, the problem is "GOOGLE CHROME". With Mozilla, everything works perfectly.

I just installed a completly new version of Laravel. Create (not copy) a Controller and a Model and just paste my existing controller code itself. Only code, no namespace, whatelse...

Truncate the table.

Run the page again. Firstly, run the page with Chrome, and the extended regists happened... Truncate once again the table. Run the page again, now with Mozilla... worked!

Clear all stored cache memory for Google Chrome 77.0.3865.90 (official Version) 64 bits, repeat all the steps, and... extended regists happened. There is something weird here on my Chrome browser, an extension, I dont know, or something else causing this problem and only for "update", because for queries e.g: select, insert or delete - works perfectly as always.

Can you imagine that?

Nakov's avatar

@luyznascimento I really cannot imagine that, but I am not saying it cannot happen. Make sure it is not an extension in your Chrome though. Try it in an incognito window.

luyznascimento's avatar

I have enable laravel QueryLogger into app\Providers\AppServiceProvider.

And below is the "query.log" register since the page (Controller) finishes loading:


2019-09-30 15:43:21 - select `reg` from `tickets` where (`control` = ? and `status` = ?) limit 3 offset 0 [1569861147, 0]
2019-09-30 15:43:21 - update `tickets` set `user` = ?, `status` = ?, `tickets`.`updated_at` = ? where `reg` in (?, ?, ?) [1, 1, 2019-09-30 15:43:21, 1900324326, 1900318869, 1900324772]
2019-09-30 15:43:21 - select * from `tickets` where (`user` = ? and `status` = ? and `control` = ?) [1, 1, 1569861147]

I have splited the query.log for easy reading. So, above is the only result I would receive. And below, unecessary behavior only in Chrome!

2019-09-30 15:43:22 - select `reg` from `tickets` where (`control` = ? and `status` = ?) limit 3 offset 0 [1569861147, 0]
2019-09-30 15:43:22 - update `tickets` set `user` = ?, `status` = ?, `tickets`.`updated_at` = ? where `reg` in (?, ?, ?) [1, 1, 2019-09-30 15:43:22, 1900324776, 1900311008, 1900315601]
2019-09-30 15:43:22 - select * from `tickets` where (`user` = ? and `status` = ? and `control` = ?) [1, 1, 1569861147]

So, the page executes twice the code...

Nakov's avatar

@luyznascimento it does but twice with different values for the whereIn which makes it update all the rows I guess.. So please share the full method code, or try to debug yourself by dumping the request input .. Check the network tab as well.. why you have two requests with different data..

luyznascimento's avatar

Nakov, as you well have noticed, there are different values as the page was loaded twice. But only happens with Chrome. The method is:

class UpdateDistributionController extends Controller
{
    public function nakov(){
        $control = 1569861147;
        $qty = 30;
        $_date = \Carbon\Carbon::now();
        $x = Ticket::select('reg')->where([ ['control', $control], ['status', 0] ])->offset(0)->take(3)->get();
        $_arr = $x->pluck('reg');
        $q = Ticket::whereIn('reg', $_arr)->update(['user' => 1, 'updated_at' => $_date, 'status' => 1]);

        print (Ticket::where([ ['user', 1],['status', 1], ['control', $control]])->get());
    }
}

And you did not read wrong! :-) The method is called "nakov"! A way to honor your help until now.

Nakov's avatar

:D so basically there is a duplicate request for some reason.. if it happens in incognito mode as well, then it is not from an extension, and i am out of ideas :)

luyznascimento's avatar

@nakov

The query (select) with eloquent done as the last part of the entire process results with only 3 results. So, as for some reason, until that moment in "memory" everything is fine... but after this line is crossed...Chrome "calls" the page "internally" again, so getting a fresh new "reg´s" values...

Nakov's avatar

It cannot be internally, there's got to be a request that happens.. How do you call this function from your view??

Try die the result at the end, change your last part of the code with this:

$_arr = $x->pluck('reg');
dump($_arr);

$q = Ticket::whereIn('reg', $_arr)->update(['user' => 1, 'updated_at' => $_date, 'status' => 1]);

dd(Ticket::where([ ['user', 1],['status', 1], ['control', $control]])->get());

This will print the ids first and finish the request.

If this works then remove the dd and add a return 'Ok'; at the end of the function..

Snapey's avatar

Are your requests put/patch or post, or are you (horror) updating the database from a GET route?

jlrdw's avatar

As I mostly use normal SQL, is it alright in laravel to have a paginating, plucking, updating query all-in-one operation like that.

I usually just paginate to display data, and if I want an edit button I add one.

And is it okay to do the updating if that is a get request is some questions you have to ask yourself.

Sorry I've just never seen anything combined like that.

offset(0)->take(3)->get();

Is there a next page.

luyznascimento's avatar
luyznascimento
OP
Best Answer
Level 1

@jlrdw

This is not the problem. I am using the same way now and working.

@nakov :

#1 Removed Chrome. Installed again without sync with my account. Everything worked! #2 Sync Chrome with my account - the problem began again!

#3 Disable every extension and repeat the process again until find that SAFE TORRENT was causing the problem! Remove that extension, run the page again, and everything worked properly!

And now I am doing update inside a loop because it is necessary to attribute the "user id" in block of 30 (here to present you all, I just considered take 3 regists) and everyhting still working as expected.

The big lesson :

Once the code part is over, it is good to consider the browser and going a little deeper, continue eliminating its extensions one by one, repeat the routine and see the result.
1 like
Snapey's avatar

still points to a problem with your code. No plugin should have this effect unless you are making changes on a GET route. There, a plugin performing prefetch can cause issues.

Please or to participate in this conversation.