-1

i wanna update my data using array, but its error 'cause im using "like" query while finding the data. This is the code.

$pin = 10590;
$datetime = "2017-08-29 15:05:00";
$date = explode(' ', $datetime);
Attendance::where('user_id', $pin)->where('datetime_in', 'like', '%' . $date[0] . '%')->update([
   'datetime_out' => $datetime,
   'updated_at' => date('Y-m-d')
]);

any solutions ?? Thanks.

6
  • 3
    There should not be an error. can you add detailed output of debugger? Commented Aug 29, 2017 at 18:38
  • @Buglinjo the server is down right now so i cant show the output, but if i run the code in mysql, the error is like this Incorrect datetime value: '%2017-08-29%' for column 'datetime_in' Commented Aug 29, 2017 at 19:04
  • You should be using like filter on varchar or text not datetime. On datetime simply use = Commented Aug 29, 2017 at 19:05
  • @Buglinjo like this? UPDATE attendance SET datetime_out="2017-08-29 15:06:00" WHERE user_id = 201037917001 AND datetime_in = "2017-08-29"; Commented Aug 29, 2017 at 19:10
  • Like this: Attendance::where('user_id', $pin)->where('datetime_in', '=', $date[0])->update([ 'datetime_out' => $datetime, 'updated_at' => date('Y-m-d') ]); Commented Aug 29, 2017 at 19:13

1 Answer 1

0

You cannot compare a datetime column using like.

Comparing using =, as suggested in the comments will also not yield the desired result, interpolating 2017-08-29 to 2017-08-29 00:00:00 (see SO Answer).

1. Cast column to DATE

Assuming you're using MySQL, you can cast the type of the queried column to DATE like this:

$query->whereRaw('DATE(datetime_in) = ?', [$day])…

PostgreSQL:

$query->whereRaw('datetime_in::date = ?', [$day])…

2. WHERE BETWEEN

Alternatively, you can query using whereBetween and specify start end end of day as time by using Carbon.

use Carbon\Carbon;

$startOfDay = new Carbon($datetime)->startOfDay();
$endOfDay = new Carbon($datetime)->endOfDay();

$query->whereBetween('datetime_in', [$startOfDay, $endOfDay])…
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.