drl37530 2016-03-30 15:39
浏览 31
已采纳

按date_created搜索学生Laravel

I want to search the "Students" table by "dateCreated" until today, in a form input in laravel 4.2.

Here is the code I have written so far. I dont know how to query this and I get an error.

View blade:

<div class="form-group">
{{ Form::label('date_created','...') }}
 <div class="input-group date">
 <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
 {{ Form::input('date_created','date_created',null,['class'=>'form-control datepicker','data-date-format' => 'mm/dd/yy']) }}
                        </div>
                    </div>

Controller

public function search(){

    $builder = User::query();.....

    if(Input::has('date_created')){
        $date = Input::get('date_created');
        $builder->select('*')
            ->where('Home_Students.dateCreated',$date)
            ->get();
    }


    $users = $builder->orderBy('home_lastname')->paginate(50);


        return View::make('user.show')->with(array('users' => $users));
    } //end function

The error I am getting

The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value

The date that is stored to the table has this form:

2010-11-10 10:54:07.077

To summarize, I want to ignore the time and just get the Students that are created, for example, from 3/07/2015 until today.

  • 写回答

3条回答 默认 最新

  • doujiao9426 2016-03-30 16:10
    关注

    I think you're missing a bit of the logic on this search. If you want dates between a given date and today, you need to reflect that in your where() clause, or use the whereBetween() function of Laravel. Also, I would format your dates to the same format as stored in the database, so try this:

    if(Input::has('date_created')){
        $date = date("Y-m-d", strtotime(Input::get('date_created')));
        $builder->select('*')
            ->whereBetween('Home_Students.dateCreated',[$date, date("Y-m-d")])
            ->get();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)