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 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊