我做了一个函数,可以在发布的文章中添加评论,然后将该函数放入ArticlesController
中。最后调用完该函数后,我尝试为该特定文章返回show()
函数,然后尝试调用$this->show('passing in params')
,但调用该函数却给了我空白页面,我现在很困惑调用和返回函数之间是否有任何区别,这是执行此类操作的正确方法?
class ArticlesController extends Controller
{
public function show(Article $article, Comment $comments)
{
$comments = $article->comments;
return view('articles.show', compact('article', 'comments'));
}
public function addComment(Article $article, Request $request)
{
$this->validate($request, [
'comment' => 'required|min:3|max:255'
]);
$comments = new Comment($request->all());
$comments->article_id = $article->id;
Auth::user()->comments()->save($comments);
return $this->show($article, $comments);
}
routes.php
<?php
Route::post('articles/{articles}', 'ArticlesController@addComment');
Route::resource('articles', 'ArticlesController');
Route::get('tags/{tags}', 'TagsController@show');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController'
]);