Flex
Flex
1 month ago (3,770 XP)

@tisuchi this is My Controller code

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();
        //
    }
tisuchi
tisuchi
1 month ago (262,165 XP)

Hi @Flex

I think this is my final thought about your issue. I just made simplified your code as much as I can.

In your web.php route file

//basically it will show a new subtask adding form
Route::get('projects/{projectId}/task/{taskId}/subtask', '[email protected]')->name('show.add.subtask.form');

In your SubtasksController

public function showAdd($projectId,$taskId)
{
    return view('subtask.form')
            ->with([
                'projectid' => $projectId,
                'taskid'    => $taskId,
            ]);
}

In subtask/form.blade.php view file, I have changed a lot. I use simple form post action and I used hidden field for projectid and task id.

<h4>Add Sub Task</h4>
  <form class="form-vertical" role="form" method="POST" action="{{ route('subtasks.form') }}">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    

    <input type="hidden" name="projectid" value="{{ $projectid }}">
    <input type="hidden" name="taskid" value="{{ $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>
    
</form>
</div>

In your web.php route file, add post action now-


Route::post('add-subtask-in-task-inside-a-projects', '[email protected]')->name('subtasks.form');


// You don't have to pass parameters in url for that since you make our parameter as hidden.

In your SubtasksController, do action method for adding-

public function store(Request $request)
{
    //do your form validation


    //just receive hidden form values
    $projectId = $request->input('projectid');
    $taskId = $request->input('taskid');


    //add new subtask
    $subtask = new Subtask;
    $subtask->subtask_name = $request->input('task_name');
    $subtask->task_id = $taskId;
    $subtask->project_id = $projectId;
    $subtask->save();
    

    //return whereever you want. 
}

Note: I can't see any issue here. Although you get any issue, you just check documentation. https://laravel.com/docs/5.5/

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