Ajax Datatable无法使用Datepicker正确搜索

时间:2019-01-03 14:05:02

标签: php jquery ajax laravel datatable

我正在Laravel中与数据表和Ajax一起构建函数。我有一个功能齐全的页面,其中包含多个函数,这些函数会将数据返回到数据包中,但是其中一个函数不喜欢正确搜索,也不会将任何数据返回到表中。

我有一个使用以下代码的数据选择器:

<div class=" input-group mb-3">
    <div class="input-group-prepend">
         <span class="input-group-text"><i class="fas fa-clock"></i></span>
    </div>
    <input class="form-control" type="text" data-plugin-datepicker id="lastLogged" name="lastLogged" placeholder="Owner has not logged in since:">
</div>

我的搜索按钮称为:search_data

我的ajax调用如下:

<script>
$(document).on("click", "#search_data", function (e) {
    e.preventDefault();
    $(this).html("<i class='fa fa-circle-o-notch fa-spin'></i>");
    })

    $.ajax({
        url: "/search/user",
        type: "POST",
        dataType: "json",
        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
        data: {
            loginUser : $('#lastLogged').val()
        }
    })

    .done(function(data) {
        console.log(data);
        table.ajax.reload();
        $('#search_data').html('<i class="fas fa-search"></i>');
    })
    .fail(function(data) {
        console.log(data);
        $('#search_data').html('<i class="fas fa-search"></i>');
    });
});

我的PHP控制器:

public function Building(Request $request)
{
    if ($request->ajax()) 
    {           
        $buildings = building::with('ownerinfo');
        $data = array();
        $totalData = building::count();
        $totalFiltered = $totalData; 
        $start = $request->input('start');
        $order = 'id';
        $dir = $request->input('order.0.dir'); 

        // Other if statements here with functions

        if(isset($request->login))
        {
            $date = date("Y-m-d H:i:s",strtotime($request->login));
            $users = Users::where('last_login_in', '<=', $date)->get();
            foreach($users as $user) {

            $buildings = $buildings->where('owner', $user->id);
            }
        }

        $buildings = $buildings->get(); 
        if(!empty($buildings))
        {
            foreach ($building as $building)
            {
                $nestedData['id'] = $building->id;
                $nestedData['name'] = $building->buildingName;
                $nestedData['view'] = '<a class="button is-small full-width is-hovered" href="/view/building/' . $building->id . '">View</a>';
                $data[] = $nestedData;
            }

            $json_data = array( 
                "data" => $data,
                "draw"            => intval($request->input('draw')),  
                "recordsTotal"    => intval($totalData),
                "recordsFiltered" => intval($totalFiltered)
            );
            return json_encode($json_data);
        }
    }
}

这将继续不返回任何数据。我正在使用datepicker的2019年1月1日进行搜索,其值是:2019-01-01 00:00:00,数据库中用户之一的数据为2018-08-20 07:11:34。我使用var_dumps检查了查询,它返回了正确的用户,但是在建筑物数据表中未显示任何结果。

其背后的想法是让管理员选择一个特定的日期,数据库运行搜索并返回自所选日期以来未登录的用户的建筑物。

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

$users返回正确的用户,但是$buildings为空? 尝试在用户和建筑物之间使用hasMany关系,这将是比这更好的解决方案:

foreach($users as $user) { $buildings = $buildings->where('owner', $user->id); }