Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 : How to retrieve records according to date?

I have a table called entries and its created_at is defined as DATE type. How do I get a record, for example, of 15 March?


What I've tried

        $post = Entry::where(\DB::raw('DATE(created_at)'),'=',date('2015-03-15'))->get()->toJSON();

but it returns empty result.


Migration

    Schema::create('entries', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('intern_id');
        $table->integer('company_id');
        $table->longText('content');
        $table->date('created_at')->unique();
        $table->date('updated_at')->unique();
    });

There are currently 3 records with dates of each of 2015-03-15, 2015-03-19 and 2015-03-20

like image 436
John Evans Solachuk Avatar asked Jul 08 '26 00:07

John Evans Solachuk


2 Answers

Use this

$post = Entry::where("created_at","2015-03-15")->get()->toJSON();
like image 195
I'm Geeker Avatar answered Jul 10 '26 15:07

I'm Geeker


You can use this form:

$post = Entry::where('created_at', '>=', '2015-03-15')->get()->toJSON();

Carbon Update

    $date = \Carbon\Carbon::create(2015, 3, 13, 0, 0, 0);

    $u = Entry::where('created_at', '>=', (string) $date)
        ->where('created_at', '<', (string) $date->addDays(1))
        ->get()
        ->toJSON();
like image 36
Chris Avatar answered Jul 10 '26 13:07

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!