'Undefined property: App\Http\Controllers\CommentController::$comment

I have a small project in which there should be a comment for each post. I tried to do this using the following code.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Comment;
use App\Repository\ICommentRepository;

class CommentController extends Controller
{
    /**
     * Write Your Code..
     *
     * @return string
     */
    public function store(Request $request)
    {
        $request->validate([
            'body'=>'required',
        ]);

        $data = $request->all();
        $data['user_id'] = auth()->user()->id;
        $this->comment->createComment($data);
        return back();
    }
}

With the following error ‍‍Undefined property: App\Http\Controllers\CommentController::$comment

After a bit of searching, I realized my mistake and corrected it as follows

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Comment;

class CommentController extends Controller
{
    public function __construct(IAdminRepository $comment){
        $this->comment = $comment;
    }
    /**
    * Write Your Code..
    *
    * @return string
    **/
    public function store(Request $request)
    {
        $input = $request->all();
        $request->validate([
            'body'=>'required',
        ]);
        $input['user_id'] = auth()->user()->id;
        $this->comment->createPost($input);
        return back();
    }
}

Then I got the same error as before and it showed me the same code while I was changing the contents of the code

enter image description here

I tried to clear the cache with php artisan config:clear and php artisan optimize but nothing changed. pleas help me



Solution 1:[1]

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Comment;

class CommentController extends Controller
{

    private $comment;

    public function __construct(IAdminRepository $comment){
        $this->comment = $comment;
    }
    /**
    * Write Your Code..
    *
    * @return string
    **/
    public function store(Request $request)
    {
        $input = $request->all();
        $request->validate([
            'body'=>'required',
        ]);
        $input['user_id'] = auth()->user()->id;
        $this->comment->createPost($input);
        return back();
    }
}

you did not define the $comment variable and you are trying to assign value to it.

Solution 2:[2]

You need to declare variable comment before the line for the constructor. i.e

private $comment; //this line is important and it was missing in your code

public function __construct(IAdminRepository $comment){
    $this->comment = $comment;
}

Solution 3:[3]

here is what she need

private $comment; 

public function __construct(IAdminRepository $comment){
    $this->comment = $comment;
}
if this is not working then you have something wrong in you IAdminRepository class

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Emeka Okafor
Solution 2 Samuel Olufemi
Solution 3