Laravel - Carbon - Errors parsing datetime to extract month

Need some help here. Let me say that this might be very simple to answer, but i'm not a programmer and started doing an app with laravel without learning the basics of php.

So, i am using Carbon to parse a datetime, to extract the month in it, and i am getting some errors. Here's what i want to parse:

 DB::table('task_interactions')->whereIn('task_id', $tasksKc)->pluck('created_at') 

i'm doing it inside a query, like this:

->where(Carbon::parse(DB::table('task_interactions')->whereIn('task_id', $tasksKc)->pluck('created_at'))->month,'=', $december)

the error i got was:

Exception in Carbon.php line 291:
DateTime::__construct(): Failed to parse time string (["2017-12-07 10:03:12",....

Did some search and found that the problem was the "" and i had to trim the string, like this:

where(Carbon::parse(trim(DB::table('task_interactions')->whereIn('task_id', $tasksKc)->pluck('created_at'),'"'))->month,'=', $december)

... and got the same error. So, I've isolated the query to dd() it in the view, and still, i cannot understand what's wrong. if i dd this in the view

$test = DB::table('task_interactions')->whereIn('task_id', $tasksKc)->pluck('created_at');

i get:

Collection {#460 ▼
  #items: array:657 [▼
    0 => "2017-12-07 10:03:12"
    1 => "2017-12-07 10:03:46"
    2 => "2017-12-07 10:04:20"
    3 => "2017-12-07 10:05:08"
    4 => "2017-12-07 10:06:29"
    5 => "2017-12-07 10:07:39"
    6 => "2017-12-07 10:09:58"
    7 => "2017-12-07 10:13:25"
    8 => "2017-12-07 10:13:57"
    9 => "2017-12-07 10:14:28"
    10 => "2017-12-07 10:15:10".....

and if i dd this:

$test = trim(DB::table('task_interactions')->whereIn('task_id', $tasksKc)->pluck('created_at'),'"');

i get:

"["2017-12-07 10:03:12","2017-12-07 10:03:46","2017-12-07 10:04:20","2017-12-07 10:05:08","2017-12-07 10:06:29","2017-12-07 10:07:39","2017-12-07 10:09:58","2017 ▶"

Any clues?

Thanks in advance

Upvotes: 1

Views: 323

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

You can do this inside the query. Use the whereMonth() method instead:

->whereMonth('created_at', 12)

Upvotes: 1

Related Questions