Nicolas Decroos Nicolas Decroos - 1 year ago 158
MySQL Question

Laravel - Advanced Carbon Date Query

I'm trying to write a certain query but I'm failing with it and I'm looking for some help

Here's what I want to do

SELECT all items WHERE created_at is from before this month (July, June,...), and also select the first 3 which are created during this month

this is what I currently have. I've tried numerous times, but I can't figure out the right "WHERE" case

$offertes = DB::table('offertes')
->where('receiver_id', $user_id)

Answer Source

Something like this should work:

$time = new Carbon\Carbon('first day of this month'); // Get first day of the month.
$time->setTime(0, 0, 0); // Set time to 00:00:00.
->where('created_at', '<', $time)
->where(function($q) {
    $q->where('created_at', '>', $time)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download