Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Amalmax's avatar

How to print project related comments in Laravel 5.2?

I need to print my comments table comments related to each project in my Laravel application. I am using domPDF for my PDF class. This is my PDF print controller:

class pdfController extends Controller
{
    public function getPDFFF($id){
        $comments = Comment::project($id)->get(); //line 14
        $pdf = PDF::loadView('pdf.out',['comments'=>$comments]);
        return $pdf->stream('comment.pdf');
    }
    //
}

This is my comments table structure:

id  comments  project_id
 1      asc                     1
 2     fgt                      5
 3     gft                      2

But with this controller I get the following error message:

ErrorException in pdfController.php line 14: Non-static method App\Comment::project() should not be called statically, assuming $this from incompatible context

how can fix this problem?

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

Assuming the $id passed is related to the project then

        $comments = Project::findOrFail($id)->comments()->get();

or

        $comments = Comment::where('project_id',$id)->get();

I would be surprised if you could print the PDF without needing to mention attributes from the project, so better to pass the project AND its comments to the PDF

    $project = Project::with('comments')->findOrFail($id);

        $pdf = PDF::loadView('pdf.out',['project'=>$project]);'

1 like

Please or to participate in this conversation.