I am developing project management app using Laravel. in my application I have project and one project have many tasks and one task have many comments. so, I have comment table like this,
id comment project_id
1 abc 1
2 dcf 1
3 fgt 2
4 fgt 2
5 fhgt 1
this is my comment controller for comment post
public function postNewComment(Request $request, $id, Comment $comment)
{
$this->validate($request, [
'comments' => 'required|min:5',
]);
$comment->comments = $request->input('comments');
$comment->project_id = $id;
$comment->user_id = Auth::user()->id;
$comment->save();
return redirect()->back()->with('info', 'Comment posted successfully');
}
and this is my comment model
protected $fillable = ['comments', 'project_id'];
now I need show comments of the each project when I go to the each project. how can do this?
In your Project model add method
public comments()
{
return $this->hasMany('App\Comment');
}
Now in blade you can use
@foreach($project->comments as $comment)
{{-- print your commets here --}}
@endforeach;
Here is documentation https://laravel.com/docs/5.2/eloquent-relationships#one-to-many
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.