Skip to content
Advertisement

Laravel where in an update statement in the controller

I have this function in my controller which works:

foreach($models as $model) {
    if ($model->EindDatum < Carbon::now() && $model->email_send == 0) {
        DB::table('SSL')->where('userID', Auth::id())->update(['email_send' => 1]);
        $model->user()->first()->notify(new AppNotificationsTaksComplete);
    }
}

Now I would like to change the update statement to this:

DB::table('SSL')->where('userID', Auth::id(), 'EindDatum' < Carbon::now())->update(['email_send' => 1]);

But this doesn’t work.. its in my where statement but I don’t know what I am doing wrong?

But this still updates all of the email_send columns in my database. even though it now should only update the ones in the past.

I dont get an error message

Advertisement

Answer

The where statement doesn’t work like this

->where('userID', Auth::id(), 'EindDatum' < Carbon::now())

The problem is that the where clause doesn’t know how to mach your expression.

You need to udpate your code like this

 DB::table('SSL')
 ->where('userID', Auth::id())
 ->where('EindDatum', '<', Carbon::now())
 ->update(['email_send' => 1]);

The result will be Where Userid = Auth::id() AND EindDatum < Carbon::now()

Where Auth::id() and Carbon::now are replaced with the values returned from the facades.

Edit: Made the code easier to read.

More about the where clauses

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement