在 Laravel 中使用 csrf 令牌时出现 500 内部服务器错误

500 Internal Server Error though using csrf token in Laravel

我不断收到错误 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;

            }
        }
    }   
}

为您的模式创建别名并创建一个条件块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();