在Laravel中验证Input :: json()

Rya*_*yan 1 laravel angularjs angular-resource

验证不适用于Input :: json.我尝试过使用json_decode /使用数组的不同方法,但仍然没有运气.这是我的代码:

//routes.php
Route::get('create', function() {

    $rules = array(
        'username' => 'required',
        'password' => 'required',
    );

    $input = Input::json();
    //var_dump($input); //outputs std obj with correct inputs
    $validation = Validator::make($input, $rules);
    if ($validation->fails()) { //throws exeption "Call to a member function to_array() on a non-object"
        return Response::json($validation->errors->all());
    }

}
Run Code Online (Sandbox Code Playgroud)

我正在使用Angular Resource发布数据...但它总是抛出错误"在非对象上调用成员函数to_array()"...我无法粘贴我的角度代码,因为我无法正确格式化和stackoverflow不允许我这样做.

Raf*_*lks 6

这是因为在input :: json()中返回一个对象,验证方法需要数组或eloquent对象.你可以做的是将对象转换为数组.

$input = Input::json();
$input_array = (array)$input;

$validation = Validator::make($input_array, $rules);
Run Code Online (Sandbox Code Playgroud)

更新:

在与@Ryan讨论之后,我注意到问题不是来自验证,而是在响应:: eloquent()中传递了一个数组而不是一个雄辩的对象.