虽然在Laravel中使用csrf令牌,但500内部服务器错误

时间:2018-01-11 11:07:09

标签: php jquery ajax laravel-5.3

我不断收到错误POST http://localhost:8000/live_datatable_search 500 (Internal Server Error),其中live_datatable_search是路由名称。虽然我使用{{ csrf_field() }}

这是我的脚本部分

var search = $('#search').val();
        var token = $('input[name=_token]').val();
        console.log(token);

        $.ajax({
                url:'/live_datatable_search',
                type: 'POST',
                data: {
                        search : search,
                        _token : token
                    },

                success:function(msg){
                    console.log(msg);

                    $("#report").html(msg);

                }
            });

路线

Route::post('live_datatable_search','HomeController@live_datatable_search');

控制器代码

public function live_datatable_search(Request $request)
{
    if($request->search)
    {
        $search = Adminuser::select('adminuser.*', 'user_type.name as user_type')
            ->orderBy('adminuser.user_type_id')
            ->leftJoin('user_type', 'adminuser.user_type_id', '=', 'user_type.id')
            ->where('adminuser.full_name', 'like', '%'. $request->search.'%')
            ->orWhere('adminuser.username', 'like', '%'. $request->search.'%')
            ->orWhere('adminuser.email', 'like', '%'. $request->search.'%')
            ->get();

        if($search)
        {
            foreach ($search as $key => $value) {

                $trHTML += "<tr class='itemList' data-toggle='modal' data-target='#exampleModal'>";
                $trHTML += "<input type='hidden' id='id' value='" + $value->id +"'><input type='hidden' id='user_type_id' value='" + $value->user_type_id + "'>";
                $trHTML += "<td id='full_name'>" + $value->full_name + "</td>";
                $trHTML += "<td id='email'>" + $value->email + "</td>";
                $trHTML += "<td id='user_type'>" + $value->user_type + "</td>";
                $trHTML += "</tr>";

                echo $trHTML;

            }
        }
    }   
}

table

1 个答案:

答案 0 :(得分:0)

制作模式的别名并制作条件orWhere。您的laravel查询中的更改,如下所示

$search = Adminuser::from("adminuser as au")
    ->leftJoin('user_type as ut', 'au.user_type_id', '=', 'ut.id')
    ->where(function($query) {
        $query->orwhere('au.full_name', 'like', '%'. $request->search.'%');
        $query->orWhere('au.username', 'like', '%'. $request->search.'%');
        $query->orWhere('au.email', 'like', '%'. $request->search.'%');
    })
    ->select('au.*', 'ut.name as user_type')
    ->orderBy('au.user_type_id')
    ->get();