I want to fetch records from table Tasks, on the basis of project_id. But i am unable to found my desired result. My Project Model code:
class Project extends Model
{
function progress(){
return $this->hasMany(Progress::class);
}
}
Progress Model Code:
class Progress extends Model
{
function project(){
return $this->belongsTo(Project::class);
}
function task(){
return $this->hasMany(Task::class);
}
}
Task Model Code:
class Task extends Model
{
protected $fillable = [
'progress',
];
function progress(){
return $this->belongsTo(Progress::class, 'id');
}
}
Here is my Controller Function:
public function task_project_view_detail($id){
$project = Project::with(['user', 'image', 'document', 'progress.task'])->where('id', $id)->first();
$progress = $project->progress;
return view('admin.tasks.project-view', ['progress' => $progress])->with('project', $project);
}
and Here is my View File:
@foreach ($progress as $prog)
@foreach ($prog->task as $tasks)
<li class="task">
<div class="task-container">
<span class="task-action-btn task-check">
<a href="" style="background:#3d981f;" class="action-circle large complete-btn" title="Mark Complete">
<i class="material-icons">check</i>
</a>
</span>
<span class="task-label" contenteditable="true">{{ $tasks->name }}</span>
<span class="task-action-btn task-btn-right">
<a href="" class="action-circle large" title="Delete Task">
<i class="material-icons">delete</i>
</a>
</span>
</div>
</li>
@endforeach
@endforeach
and Here is my result: there should be only first 3 rows, but all tasks in this project are showing.
There should be only 1 row, the 4th one.
Here is Schema of Project Model:
Schema::create('projects', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('user_id')->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('title');
$table->timestamps();
});
Here is Schema of Progress Model:
Schema::create('progress', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('project_id')->nullable();
$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade')->onUpdate('cascade');
$table->string('name');
$table->timestamps();
});
and here is Schema of Task Model:
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('progress_id')->nullable();
$table->foreign('progress_id')->references('id')->on('progress')->onDelete('cascade')->onUpdate('cascade');
$table->string('name')->nullable();
$table->string('status')->nullable();
$table->timestamps();
});
What should I do?
The has-many-through
relationship provides a convenient shortcut for accessing distant relations via an intermediate relation. In your case you can use this like:
In Project Model Define tasks()
:
public function tasks()
{
return $this->hasManyThrough('App\Task', 'App\Progress');
}
The first argument passed to the hasManyThrough
method is the name of the final model we wish to access, while the second argument is the name of the intermediate model.
And to get project task you can do something like below in controller:
public function task_project_view_detail($id){
$project = Project::with(['user', 'image', 'document', 'tasks'])->where('id', $id)->first();
$tasks= $project->tasks;
return view('admin.tasks.project-view', ['tasks' => $tasks])->with('project', $project);
}
And in view directly use foreach
as below:
@foreach ($tasks as $task)