samedi 11 janvier 2020

Laravel Patch Request doesn't update 'completed' Task status

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. 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. We have a separate controller called ProjectTasksController for handling the logic and a form in our show.blade.php view for sending the request. Any help would be greatly appreciated.

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

<a class="nav-link" href="/projects//edit"><i class="material-icons">edit</i></a>

@if ($project->image)
        <div class="row">
        <div class="col-12">
            <img src="" 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/">
         
        @csrf
             <label class="checkbox  " for="completed">
                 <input type="checkbox" name="completed" onChange="this.form.submit()"  >
                 
             </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');



Aucun commentaire:

Enregistrer un commentaire