简体   繁体   English

Laravel 补丁请求不更新数据库表

[英]Laravel Patch Request doesn't update database table

For our Task attributes we have the following: task_id as primary key, user_id, stage_id and project_id as foreign keys, completed as boolean and a description.对于我们的任务属性,我们有以下内容:task_id 作为主键,user_id、stage_id 和 project_id 作为外键,完成为布尔值和描述。 Our goal is to display the tasks under a project and by checking the checkbox right next to them, it should mark them as complete.我们的目标是显示项目下的任务,并通过选中它们旁边的复选框,将它们标记为完成。 The problem is in our database the 'complete' status doesnt change.问题是在我们的数据库中,“完成”状态没有改变。 We are using PhpMyAdmin.我们正在使用 PhpMyAdmin。 We have a separate controller called ProjectTasksController for handling the logic and a form in our show.blade.php view for sending the request.我们有一个名为 ProjectTasksController 的单独控制器用于处理逻辑,在 show.blade.php 视图中有一个用于发送请求的表单。 Any help would be greatly appreciated.任何帮助将不胜感激。

@extends('layouts.app')
@section('content')
<div class="display-3">{{$project->name}}</div>

<a class="nav-link" href="/projects/{{$project->project_id}}/edit"><i class="material-icons">edit</i></a>

@if ($project->image)
        <div class="row">
        <div class="col-12">
            <img src="{{ asset('storage/' . $project->image) }}" alt="...." class="img-thumbnail">
        </div>
        </div>
 @elseif(!$project->image)
 no image
@endif

@if ($project->tasks->count())
 <div>
     @foreach ($project->tasks as $task)
     <div>
     <form method="POST" action="/tasks/{{$task->task_id}}">
        {{method_field('PATCH')}} {{-- @method('PATCH') --}}
        @csrf
             <label class="checkbox {{$task->completed ? 'is_complete' : ''}} " for="completed">
                 <input type="checkbox" name="completed" onChange="this.form.submit()" {{$task->completed ? 'checked' : ''}} >
                 {{$task->description}}
             </label>
         </form>

        </div>
     @endforeach
 </div>
 @endif

@endsection
<?php

namespace App\Http\Controllers;

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

class ProjectTasksController extends Controller{

public function update(Task $task)
{
    $task->update([
        'completed' => request()->has('completed')
    ]);

    return back();
}


}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    protected $guarded = [];
    protected $primarykey = ['task_id'];
    protected $fillable = ['user_id','stage_id','project_id','completed','description'];
    public function stage(){

        return $this->belongsTo(Stage::class);

    }

    public function user(){

        return $this->belongsTo(User::class);

    }


    public function project(){

        return $this->belongsTo(Project::class);

    }

    }
{
_method: "PATCH",
_token: "ljiwu8bEtAkRqSUOXllmaRbSujavHNYNRJR5TMcy",
completed: "on"
}
Route::patch('/tasks/{task_id}', 'ProjectTasksController@update');

Your controller method was not correct, hint of Task $task is just a instance of Task not the collection or a single Model.And you have not specify your Request $request to get this work request()->has('completed') in method arguments.You need to edit your method in following way:您的控制器方法不正确, Task $task提示只是Task一个实例,而不是集合或单个模型。您还没有指定Request $request来获取此工作request()->has('completed')在方法参数中。您需要按以下方式编辑您的方法:

public function update(Request $request,$task_id)
    {
        Task::find($task_id)->update([
            'completed' => $request->has('completed')
        ]);

        return back();
    }

Note: $request->has('completed') will return Boolean ;注意: $request->has('completed')将返回Boolean if you want exact value,then you need to retrieve as $request->get('completed')如果你想要确切的值,那么你需要检索为$request->get('completed')

If you want to use route model binding the name of your parameter in the update function should match the route parameter:如果要使用路由模型绑定,则更新函数中的参数名称应与路由参数匹配:

Route::patch('/tasks/{task}', 'ProjectTasksController@update');

Replace protected $primaryKey = ['task_id]';替换protected $primaryKey = ['task_id]'; with protected $primaryKey ='task_id' in the Task model.在 Task 模型中使用protected $primaryKey ='task_id' It should be a string, not an array.它应该是一个字符串,而不是一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM