在Laravel 5.2上从空值错误创建默认对象

时间:2016-03-11 16:21:02

标签: ajax laravel laravel-5.2

我正在使用Laravel 5.2构建应用程序。我正在尝试使用jquery创建一个Edit模式。但是,每次我尝试更新数据库中的记录时,我都会收到500内部服务器错误。在使用Firebug进一步调查时,我收到错误:

  

“从空值创建默认对象”......

这些是相关的代码块。

Route.php - 我正在尝试从我的模态

访问编辑路径
<?php

Route::group(['middleware' => ['web']], function () {

    Route::get('/', function () {
        return view('welcome');
    })->name('home');

    Route::post('/signup', [
        'uses' => 'UserController@postSignup',
        'as' => 'signup']);

    Route::post('/signin', [
        'uses' => 'UserController@postSignin',
        'as' => 'signin']);

    Route::get('/logout', [
        'uses' => 'UserController@getLogout',
        'as' => 'logout']);


    Route::get('/dashboard', [
        'uses' => 'PostController@getDashboard',
        'as' => 'dashboard',
        'middleware' => 'auth'
    ]);

    Route::post('/createpost', [
        'uses' => 'PostController@CreatePost',
        'as' => 'createpost',
        'middleware' => 'auth']);

    Route::get('/delete-post/{post_id}', [
        'uses' => 'PostController@getDeletePost',
        'as' => 'post.delete',
        'middleware' => 'auth']);

    Route::post('/edit', [
        'uses' => 'PostController@getEditPost',
        'as' => 'edit'
    ]);
});

PostController.php

public function getEditPost(Request $request)
{
    $this->validate($request, [
       'body' => 'required'
    ]);
    $post = Post::find($request['postid']);

    $post->body = $request['body'];
    $post->update();
    return response()->json(['new_body' => $post->body], 200);
}

包含点击事件app.js的Javascript文件成功更新数据库后,我正在向控制台打印消息

var postId = 0;
$('.post').find('.interaction').find('.edit').on('click', function (event) {
    event.preventDefault();
    var postBody = event.target.parentNode.parentNode.childNodes[1].textContent;
    postId = event.target.parentNode.dataset['postid'];
    $('#post-body').val(postBody);
    $('#edit-modal').modal();
});
$('#modal-save').on('click', function () {
    $.ajax({
        method: 'POST',
        url: url,
        data: {body: $('#post-body').val(), postId: postId, _token: token}
    }).done(function (msg) {
        console.log(JSON.stringify(msg));
    });
});

这是我的观看页面

dashboard.blade.php

@extends('layouts.master')

@section('content')
@include('includes.message-block')
<section class="row new-post">
    <div class="col-md-6 col-md-offset-3">
        <header><h3>What do you have to say?</h3></header>
        <form action="{{ route('createpost') }}" method="post">
            <div class="form-group">
                <textarea class="form-control" name="body" id="new-post" rows="5" placeholder="Your Post"></textarea>
            </div>
            <button type="submit" class="btn btn-primary">Create Post</button>
            <input type="hidden" value="{{ Session::token() }}" name="_token">
        </form>
    </div>
</section>
<section class="row posts">
    <div class="col-md-6 col-md-offset-3">
        <header><h3>What other people say...</h3></header>
        @foreach($posts as $post)
            <article class="post" data-postid="{{ $post->id }}">
                <p>{{ $post->body }}</p>
                <div class="info">
                    Posted by {{ $post->user->first_name }} on {{ $post->created_at }}
                </div>
                <div class="interaction">
                    <a href="#">Like</a> |
                    <a href="#">Dislike</a>
                    @if(Auth::user() == $post->user)
                        |
                        <a href="#" class="edit">Edit</a> |
                        <a href="{{ route('post.delete', ['post_id' => $post->id]) }}">Delete</a>
                    @endif
                </div>
            </article>
        @endforeach
    </div>
</section>

<div class="modal fade" tabindex="-1" role="dialog" id="edit-modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Edit Post</h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="post-body">Edit the Post</label>
                        <textarea class="form-control" name="post-body" id="post-body" rows="5"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" id="modal-save">Save changes</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script>
    var token = '{{ Session::token() }}';
    var url = '{{ route('edit') }}';
</script>
@endsection

请有人帮我看看我在这里缺少什么吗?感谢

1 个答案:

答案 0 :(得分:1)

您似乎在null上获得$post = Post::find($request['postid']);

在尝试更新模型之前进行一些检查。

您可以使用::findOrFail()或检查!is_null($post)

此外,您应该使用$request->input('postid')