d3vma
d3vma

Reputation: 171

Laravel compare between date field and date now

I am trying to compare between date field that saved to my database and current date!

The circle is:

  1. admin will add a new career with deadline date
  2. when someone fill the application he/she will see the available jobs only in drop down list ( that its deadline date less than the current date )

so this is the Jobs model

          protected $fillable = ['job_name','job_req', 'expire'];

this is jobs migrations

          public function up()
{
    Schema::create('jobs', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('job_name');
        $table->string('job_req');
        $table->date('expire');
        $table->timestamps();
    });
}

public function down()
{
    Schema::drop('jobs');
}

this is the ApplicationController.php

       public function create()
{   
    $dt = Carbon::now()->toDateString();
    $jobs = Jobs::get()->where('$dt', '<', 'expire');
    return view('post.create',compact('jobs'));
}

Now when i open application form it doesn't returns any job title, but when i remove where clause from the controller it works well!

Upvotes: 1

Views: 6559

Answers (1)

Trung
Trung

Reputation: 650

change

$jobs = Jobs::get()->where('$dt', '<', 'expire');

to

$jobs = Jobs::where('expire', '>', $dt)->get();

->get() will do query instantly, you must use ->where() before it.

Use ->where after ->get(), you will call this function http://laravel.com/api/5.1/Illuminate/Database/Eloquent/Collection.html#method_where

Upvotes: 1

Related Questions