Skip to content

Instantly share code, notes, and snippets.

@PoTHuYJoHN
Last active April 29, 2017 13:07
Show Gist options
  • Save PoTHuYJoHN/870b4e99d042420c1ee09dcd9e96ecd8 to your computer and use it in GitHub Desktop.
Save PoTHuYJoHN/870b4e99d042420c1ee09dcd9e96ecd8 to your computer and use it in GitHub Desktop.
Laravel query DB by month from related table and convert string value to date
<?php
/**
* Find Landing pages where has fields with date value by month
* Date format is mm/dd/yyyy
*/
$eventItemsIds = LandingPage::where('section', 'events_items')->whereHas('fields', function ($query) use($month,$year) {
$query->whereHas('texts', function($query) use($month,$year) {
$query->where(\DB::raw('MONTH(STR_TO_DATE(value, \'%m/%d/%Y\'))'), '=', $month)
->where(\DB::raw('YEAR(STR_TO_DATE(value, \'%m/%d/%Y\'))'), '=', $year);
});
})->lists('id');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment