控制器中的重定向错误

时间:2017-01-16 14:52:33

标签: php laravel laravel-5.3

我似乎无法解决我的问题,当我删除任务时,它将我重定向到仪表板我可以在控制器中看到它当前正在重定向到/仪表板页面但是当我尝试修复它时它只是出错:/我希望它实现创建重定向的相同结果

public function createTaskPage(Request $request, $slug) {

    $project = Project::where('slug', $slug)->firstOrFail();

    // Validate it has a body.
    $validate = validator(
            $request->toArray(), [
        'task' => 'required',
    ]);

    if ($validate->fails()) {
        return response()->json([], 400);
    }
    $tasks = new Task;
    $tasks->project_id = $project->id;
    $tasks->body = $request->task;
    $tasksCreate = Auth::user()->tasks()->save($tasks);
    //return response()->json([], 201);
    return redirect()->to('/project/' . $project->slug);
}

public function doDeleteTask($id) {
    try {
        $tasks = Task::where('user_id', Auth::user()->id)->findOrFail($id);
    } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $ex) {
        return response()->json([], 404);
    }
    $tasks->delete();
    //return response()->json([], 204);
    return redirect()->to('/dashboard');
}

这是删除按钮

的表单
<div class="col-md-3">
<div class="panel panel-default shadow">
    <div class="panel-body">
        <div class="row text-center">
            <h4><strong>{{ str_limit(strip_tags($project->name), 20) }}</strong></h4>
            <h5>{{ $project->created_at }}</h5>
        </div>
        <div class="row text-center" style="margin:5px;">
            {{ ($project->description) }}
        </div>
        <div class="row text-center">
            <form action="{{ url('/project/'.$project->id) }}" method="POST">
                {{ csrf_field() }}
                {{ method_field('DELETE') }}
                <button type="submit" class="btn btn-link btn-sm" onclick="return ConfirmDelete(this)" style="margin:5px;"><i class="fa fa-ban fa-3x" aria-hidden="true"></i></button>
                <a href="{{ url('/project/' . $project->slug) }}" class="btn btn-link btn-sm"><i class="fa fa-arrow-circle-o-right fa-3x" aria-hidden="true"></i></a>
            </form>              
        </div>
    </div>
</div>

这是我的删除路线&amp;创建路线

Route::delete('/task/{id}', 'TaskController@doDeleteTask'); // Delete a task
Route::post('/project/{slug}/task', 'TaskController@createTaskPage')->name('task');

1 个答案:

答案 0 :(得分:0)

如果您在任务中定义了项目关系,则可以执行以下操作:

var t1 = new testFunc();
var t2 = new testFunc();
.
.
.
.