douxiong2001 2015-09-06 01: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-06 01: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

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?