John Evans Solachuk
John Evans Solachuk

Reputation: 2105

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

Upvotes: 3

Views: 3016

Answers (2)

I'm Geeker
I'm Geeker

Reputation: 4637

Use this

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

Upvotes: 2

Chris
Chris

Reputation: 58312

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();

Upvotes: 2

Related Questions