how to show table data regarding to id

Published 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!

Best Answer (As Selected By Flex)
IgorBabko

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
Dry7
Dry7
1 year ago (151,880 XP)

@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

IgorBabko

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
Flex
Flex
1 year ago (8,250 XP)

@IgorBabko did it but generated following error

Undefined variable: task (View: C:\Users\Flex\Desktop\promag\resources\views\comments\form.blade.php)
Flex
Flex
1 year ago (8,250 XP)

@Dry7 later I need customized this with project_id that means comment show

related to project_id -> task_id -> comment
IgorBabko

@Flex -

You should pass $task variable to the view from your controller:

public function show(Task $task)
{
    // or if you don't use implicit route model binding
    // $task = Task::find('<taskId>');

    return view('tasks.show', ['task' => $task]);
}
Flex
Flex
1 year ago (8,250 XP)

@IgorBabko witch controller? Comment or Task

IgorBabko

@Flex -

routes/web.php

Route::get('/tasks/{task}', '[email protected]');

app/Http/Controllers/TaskController.php


namespace App\Http\Controllers;

class TaskController extends Controller
{
    public function show(Task $task)
    {
        return view('tasks.show', ['task' => $task]);
    }
}


Flex
Flex
1 year ago (8,250 XP)

@IgorBabko did it but same error massage here

see my TasksController

namespace App\Http\Controllers;

use Auth;
use App\Project;
use App\Task;
use Illuminate\Http\Request;

use App\Http\Requests;

class TasksController extends Controller
{
public function postNewTask(Request $request,$id, Project $project)
{

    $task = new Task;
    $task->task_name   = $request->input('name');
    $task->body = $request->input('body');
    $task->assign = $request->input('status');
    $task->priority = $request->input('status');
     $task->duedate  = date("Y-m-d", strtotime($request->input("date")));
     $task->project_id = $id;
$task->save();
     return redirect()->back()->with('info', 'Task created successfully');
}

public function getOneProjectTask($projectId, $taskId)
    {


     
        $task = Task::where('project_id', $projectId)
                      ->where('id', $taskId)
                      ->first();
        return view('tasks.show')->withTasks($task)->with('projectId', $projectId);
    }

public function show(Task $task)
    {
        return view('tasks.show', ['task' => $task]);
    }

}
IgorBabko

@Flex -

Can you show me your view file where you reference $task variable?

Flex
Flex
1 year ago (8,250 XP)

@IgorBabko ok it is in tasks/index.blade.php


  @foreach ($project->tasks as $task)
    <h4><a href="/projects/{{$project->id}}/tasks/{{ $task->id }}">{{ $task->task_name }}</a></h4>
@endforeach

and this index.blade.php file situated in projects/show.blade.php

@include('tasks.index')
    
IgorBabko

@Flex -

Here I don't see where you reference that $task variable that was passed from controller. In your foreach loop, you have different ( scoped only to this loop ) task variable.

But if you explicitely pass $task variable from controller like so:

public function show(Task $task)
{
    return view('tasks.show', ['task' => $task]);
}

then you will definitely have access to $task variable from the view and all partials that you include in the view.

So, please, make sure that you render an appropriate view (tasks.show) where $task variable is explicitly referenced.

If it's not clear, you can send your code sample where you explicitly reference $task variable, so I can take a look.

Flex
Flex
1 year ago (8,250 XP)

@IgorBabko dear, this is My TaskControlller method to show task in tasks/show.blade.php

 public function getOneProjectTask($projectId, $taskId)
    {


     
        $task = Task::where('project_id', $projectId)
                      ->where('id', $taskId)
                      ->first();
        return view('tasks.show')->withTasks($task)->with('projectId', $projectId);
    }

this is my tasks/show.blade.php

<h2>{{ $tasks->project->project_name }}</h2>
<hr>

{{$tasks->task_name}}
<hr>

{!!$tasks->body!!}

<hr>


@include('comments.form')


IgorBabko

@Flex -

I've noticed that you're passing task in the variable named $tasks from getOneProjectTask method.

http://d.pr/i/eDEPyc

Rename withTasks method call on withTask and you'll be able to reference task via $task variable in tasks.show as well as comments.form views.

Flex
Flex
1 year ago (8,250 XP)

@IgorBabko Grete it is working thanks for nice support

Flex
Flex
1 year ago (8,250 XP)

@IgorBabko now I need change little bit that means show tasks comment to relevant each task_id (current system show all comments regarding to project now I need show only comments regarding to each task on each project)

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