I having a list of records that have a date (like 11/5/2013
, that's a MDY formt), but the dates are VARCHAR
fields.
I want to select all the records that are in the last 30 days, but don't know how to cast them to date format, as I get NULL in dates field.
I'm using Laravel and Eloquent ORM, how can I accomplish this?
You need an accessor in your model.
Assume your have a dates
field in your table.
public function getDatesAttribute($value)
{
$this->attributes['dates'] = Carbon::createFromFormat('m/d/Y', $value);
}
The above function will convert the date from string to Carbon object. By default Laravel support Carbon.
Now from you controller:
$test = Test::where('dates', '>=', Carbon::now()->subMonth())->get();
I haven't tested the code but should work. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With