@Flex you need to use the task_id in the comments table instead of the project_id in Task model
public function comments()
{
return $this->hasMany('App\Comment');
}
and just use foreach($task->comments) in view
hi, 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 user_id
1 abc 1 1
2 dcf 1 2
3 fgt 2 1
4 fgt 2 2
5 fhgt 1 3
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
<?php
namespace App;
use App\User;
use App\Project;
use Auth;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['comments', 'project_id', 'user_id'];
public function scopeProject($query, $id)
{
return $query->where('project_id', $id);
}
/*
* Get the user that is reponsible for a comment
* @return collection
*/
public function user()
{
return $this->belongsTo(User::class);
}
public function project()
{
return $this->belongsTo('App\Project');
}
}
in my application user can visit there project area using projectController index function
public function index()
{
$projects = Project::all();
return view('projects.index')->withProjects($projects);
}
and then when click on one project link user can see tasks created form and existing tasks list for relevant project. my task created form is in projects folder in view file in resources folder and existing task list include with this file projects/show.blade.php
@include('tasks.index')
tasks/index.blade.php file is
@foreach ($project->tasks as $task)
<h4><a href="/projects/{{$project->id}}/tasks/{{ $task->id }}">{{ $task->task_name }}</a></h4>
@endforeach
my project model is
<?php
namespace App;
use Auth;
use App\Task;
use App\Comment;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
protected $fillable = ['project_name','project_notes','project_status','color','group'];
//
public function tasks(){
return $this->hasMany('App\Task');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
}
now when I click one of existing task link I can see comment box and now I need show comment table comment on each task related with each project. I mean I need to show each project comments on every tasks witch is related to that project. can you give me some help?
Thanks!
Hey -
As far as I understand you just have to output comments that are related to the project that the task belongs to.
If so then you will have to create project relationship on the Task model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Task extends Model
{
protected $guarded = [];
public function project()
{
return $this->belongsTo('App\Project');
}
}
And then if you have an access to the task object in your view, you can simply retrieve all project's comments like so:
@foreach ($task->project->comments as $comment)
{{-- do something --}}
@endforeach
Please or to participate in this conversation.