douxiong2001 2015-09-05 17:34
浏览 295
已采纳

Laravel现在比较日期字段和日期

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!

展开全部

  • 写回答

1条回答 默认 最新

  • douya5194 2015-09-05 17:52
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部