Home > OS >  Undefined property: App\Http\Controllers\CommentController::$comment
Undefined property: App\Http\Controllers\CommentController::$comment

Time:03-31

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

CodePudding user response:

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.

CodePudding user response:

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;
}

CodePudding user response:

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
  • Related