I am developing project management app using laravel 5.2 and in my application user can create project and one project have many tasks and one task have many sub tasks. this is my relationship with each models
project Model
public function tasks(){
return $this->hasMany('App\Task');
}
Task Model
public function subtasks(){
return $this->hasMany(Subtask::class);
}
Subtask Model
public function task(){
return $this->belongsTo(Task::class);
}
this is my subtask input form in subtasks/form.blade.php
Add Sub Task <form class="form-vertical" role="form" method="post" action="{{ route('subtasks.form', ['projectId'=> $projectId, 'taskId'=>$taskId])}}">
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<input type="text" name="task_name" class="form-control" id="name" value="{{ old('task_name') ?: '' }}">
@if ($errors->has('task_name'))
<span class="help-block">{{ $errors->first('task_name') }}</span>
@endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Create Task</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
route
Route::get('projects/{projectId}/task/{taskId}/subtask', function ($projectId, $taskId) {
return view('subtasks/form',['projectId'=>$projectId,'taskId'=>$taskId]);
});
in my form file I can print $projectId and $ taskId as well but now I need print project_name and task_name regarding to ids how can I do this?
updated question please see my subtask store methods
public function store(Request $request,$projectId,$taskId)
{
$subtask = new Subtask;
$subtask->subtask_name = $request->input('task_name');
$subtask->task_id = $taskId;
$subtask->project_id = $projectId;
$subtask->save();
//
}
You can do like so (Q&D solution):
Route::get('projects/{projectId}/task/{taskId}/subtask', function ($projectId, $taskId) {
$project = \App\ProjectModel::find($projectId);
$task = \App\TaskModel::find(taskId);
return view('subtasks/form',['project'=>$project,'task'=>$task]);
});
Note: In your form action, you will need to update the following :
['projectId'=> $projectId, 'taskId'=>$taskId]
By :
['projectId'=> $project->id, 'taskId'=>$task->id]