Laravel: 无法删除用户创建的 post

Laravel: Can't delete a post created by the user

因此用户可以删除他们创建的 post。然而,当我点击删除时,我收到一条错误消息,指出没有任何东西可以删除,我不明白为什么会这样。

这是错误:

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to a member function delete() on null

下面是我的post控制器class,不明白为什么$post = Post::where('id', $post_id)->first();returnsnull.

class PostController extends Controller
{
    public function getDashboard(){
        $posts = Post::all();
        return view('dashboard', ['posts' => $posts]);
    }

    public function postCreatePost(Request $request){
        $this->validate($request, [
            'body' => 'required'
        ]);

        $post = new Post(); 
        $post->body = $request['body'];
        $message = 'There was an error';
        if($request->user()->posts($post)->save($post)){; //points here
            $message = 'post successfully created';
        }
        return redirect()->route('dashboard')->with(['message' => $message]);
    }

    public function getDeletePost($post_id){
        $post = Post::where('id', $post_id)->first();
        $post->delete();
        return redirect()->route('dashboard')->with(['message' => 'post deleted']);
    }
}

这是我的 dashboard.blad.php 视图:

<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('postcreate') }}" method="post">
            <div class="form-group">
                <textarea class="form-control" name="body" rows="5" placeholder="your post"></textarea>
            </div>
            <button type="submit" class="btn btn-primary">Create post</button>
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
        </form>
    </div>
</section>

<section class="row posts">
    <div class="col-md-6 col-md-offset-3">
        @foreach($posts as $post)
            <article class="post">
                <p>{{ $post->body }}</p>
                <div class="info">posted by {{ $post->user->email }} on {{ $post->created_at }}</div>
                <div class="interaction">
                    <a href="#" class="post-item">Like</a>
                    <a href="#" class="post-item">Edit</a>
                    <a href="{{ route('post.delete', ['post_id => $post->id']) }}" class="post-item">Delete</a>
                </div>
            </article>
        @endforeach
    </div>
</section>

我的网络路线:

Route::get('/', function () {
    return view('landing');
});

Route::get('/user', function () {
    return view('User/usersignup');
});


Route::post('/signup', 'UserController@postSignUp')->name('signup');

Route::post('/signin', 'UserController@postSignIn')->name('signin');

Route::get('/dashboard', 'PostController@getDashboard')->name('dashboard');

Route::post('/createpost', 'PostController@postCreatePost')->name('postcreate');

Route::get('/delete-post/{post_id}', 'PostController@getDeletePost')->name('post.delete');

你的问题和你上一个问题几乎一样,所以我想你需要学会理解错误信息。

Call to a member function delete() on null. (On line XXX)

消息中包含了您需要知道的一切!

在线 XXX:

public function getDeletePost($post_id){
    $post = Post::where('id', $post_id)->first();
    $post->delete(); // That should be the line XXX
    return redirect()->route('dashboard')->with(['message' => 'post deleted']);
}

错误表明您正在尝试对 null 变量调用方法 delete()。这告诉您 $post 变量为空。够简单了。

好的,但为什么它是空的?

它为 null 可能是因为 Post 模型不包含给定 $post_id.

的任何条目

要避免这种可能性,您有两种方法:

1st - 在删除

之前检查 $post 是否不是 null
if ($post) {
    $post->delete();
}

**2 - 使用 firstOrFail() Eloquent 方法

$post = Post::where('id', $post_id)->firstOrFail();

如果模型不存在,这将在内部调用 abort(404)

我试过了,没成功。

所以最后一种可能是你的 $post_id 由于某种原因是空的。检查您的 http 请求。