0

I'm trying to code a simple social media page. The show view should display a post with all the comments for that particular post listed below.

I've tried a couple different approaches with no luck, this is the approach I feel like I might be closet to success with, any suggestions?

I can provide other extracts of code if you think the problem lies elsewhere but I think my problem lies within these 4 files.

web.php

Route::resource('post', 'PostController');
Route::resource('post', 'CommentController');

show.blade.php

<h1>{{$post->title}}</h1>
<p>Description: {{$post->description}}</p>

<h3>Comments</h3>
<ul>
  @foreach ($comments as $comment)
    <li>
      User: {{$comments->user_name}} <br>
      Comment: {{$comments->comment}} <br>
    </li><br>
  @endforeach
</ul>

PostController.php

public function show($id)
{
  $post= Post::find($id);
  return view('post.show')->with('post', $post);
}

CommentController.php

public function show($id)
{   
    $comments= Comment::find($id);
    return view('post.show')->with('comments', $comments);

}

EDIT 1

Post.php

class Post extends Model
{
    protected $fillable = ['title', 'description'];

    function user() {
        return $this->belongsTo('App\User');
    }

    function comment() {
        return $this->hasMany('App\Comment');
    }

}

4
  • you want to get the show view with two types of data right ? Commented Sep 30, 2018 at 7:05
  • @EmtiazZahid Essentially yes Commented Sep 30, 2018 at 7:06
  • so why not you making relation between two models? Commented Sep 30, 2018 at 7:08
  • use relationship between modals and visit this laravel.com/docs/5.7/eloquent-relationships#one-to-many Commented Sep 30, 2018 at 7:14

2 Answers 2

1

firstly set the relationship between Post and Comment Model

In Post Model

class Post extends Model
{
    public function comments(){
        return $this->hasMany(Comment::class);
    }
}

In Comment Model

class Comment extends Model
{
    public function post(){
        return $this->belongsTo(Post::class);
    }
}

Post Controller

public function show($id)
{
  $post= Post::find($id);
  return view('post.show')->with('post', $post);
}

Write in the blade file

<h1>{{$post->title}}</h1>
<p>Description: {{$post->description}}</p>

    <h3>Comments</h3>
    <ul>
      @foreach ($post->comments as $comment)    //get all the comments related to this post
        <li>
          User: {{$comment->user_name}} <br>
          Comment: {{$comment->comment}} <br>
        </li><br>
      @endforeach
    </ul>
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for that Sunil. However, after implementing that I get a Undefined variable: post error?
I think you are not passing the $post variable to the blade file.
and i updated the code please check again!. @Jake2018
I now get Invalid argument supplied for foreach()
@Jake2018 check your method name in Post Model. If it is comment() then write $post->comment and if it is comments() then write $post->comments.
|
1

You can get both data by make relations between your models

I think in your two models the relation would be like that,

Post Model

namespace App\Post;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function comments(){
        return $this->hasMany(Comment::class);
    }
}

Comment Model:

namespace App\Comment;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    public function post(){
        return $this->belongsTo(Post::class);
    }
}

so if you want to return the data to view then,

PostController.php

 public function show($id)
    {
      $post= Post::with('comments')->find($id);
      $data = [
            'post' => $post,
            'comments' => $post->comments,
      ];
      return view('post.show', $data);
    }

CommentController.php

 public function show($id)
{   
    $comments= Comment::with('post')->find($id);
    $data = [
        'post' => $comments->post,
        'comments' => $comments,
    ];
    return view('post.show' , $data);
}

For more details: https://laravel.com/docs/5.7/eloquent

2 Comments

Thanks for that. However I get a 'Trying to get property of non-object' error for the show.blade.php file with this code
then it can be the problem of data. please debug using dd function when you returning the data in your view. example place dd($post); before return in your show function in post controller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.