LoginSignup
3
0

More than 5 years have passed since last update.

Laravel5.5のroute:listでCall to a member function parameter() on nullと言われた

Posted at

前提

基盤にしているのはこちらの記事

ついでにこれを見ながらあれこれメモした記事。

事象

記事にコメントを書けるようにしたかったのです。
というわけでCommentControllerとかいうのを追加して、postに紐付いたcommentを保存する機能を追加しました。

CommentController.php

CommentController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use League\Flysystem\Exception;
use Illuminate\Support\Facades\Route;

class CommentController extends Controller
{
    public function __construct(Request $request)
    {
        $this->middleware('auth');
    }

    public function store(Request $request)
    {
        $comment = new Comment();

        $comment->comment = $request->comment;

        $comment->user_id = $request->user()->id;
        $comment->post_id = $request->post_id;
        $comment->save();

        return redirect('posts/'.$comment->post_id)->with('status', __('Commented to article.'));
    }
}

web.php

web.php
Route::post('posts/{post_id}/comments', 'CommentController@store');

route:listでエラー

すると、php artisan route:listでなんかエラーになるようになりました。

$ php artisan route:list

In Router.php line 991:

  Call to a member function parameter() on null  

ちなみに動作自体は想定通りに動いてます。
なんでだろう。

原因

とりあえず、web.phpに追加したルーティングをコメントアウトするとエラーは出ません。
なのでこれが原因っぽいです。

対応

storeがダメなのかな?と思って名前を変えてみるも特に変化なし。

いろいろ試した結果、この機能をPostControllerに移動させたらエラーが出なくなることがわかりました。
(storeのままだとかぶるので、storeCommentとかに変更しました)

んー……なんとなくわかるようなわからないような……。

まあそもそも分けた理由自体なんとなくというかぶっちゃけ理由なんてなかったんで、どうでもいいんですけどいまいち腑に落ちない。

3
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0