因此,用户可以删除他们创建的帖子。但是,当我点击删除时,我收到一条错误,指出没有任何内容可以删除,我无法弄清楚为什么会这样。
这是错误:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to a member function delete() on null
以下是我的帖子控制器类,我不明白为什么$post = Post::where('id', $post_id)->first();
会返回null
。
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');
答案 0 :(得分:1)
您的问题几乎与上一个问题相同,所以我认为您需要学会理解错误信息。
在null上调用成员函数delete()。 (在第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']);
}
错误表明您尝试在delete()
变量上调用方法null
。这告诉您$post
变量为空。很简单。
好的,但为什么它是空的?
它可能是因为Post
模型不包含给定$post_id
的任何条目。
为避免这种可能性,您有两种方法:
1st - 在删除之前检查$post
是否不是null
if ($post) {
$post->delete();
}
** 2nd - 使用firstOrFail()
Eloquent方法
$post = Post::where('id', $post_id)->firstOrFail();
如果模型不存在,这将在内部调用abort(404)
。
我尝试过,但没有成功。
所以最后一种可能性是你的$post_id
由于某种原因是空的。检查您的http请求。