Flex
63
18
Laravel

how to show table data regarding to id

Posted 1 year ago by Flex

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!

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.