0

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">{{$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');

2 Answers 2

4

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:

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; if you want exact value,then you need to retrieve as $request->get('completed')

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you it worked for the task completion! But now for adding the tasks it gives us the following error, we changed the routes a bit and the show.blade.php page. Can you help us with that too, we are really stuck, thank you in advance!
public function store(Project $project) { //$project->addTask(request('description')); Task::create([ 'project_id' => $project->project_id, 'description'=>request('description'), 'user_id' => auth()->id(), ]); return back(); }
Route::resources(['projects'=> 'ProjectController', 'profile' => 'ProfileController', 'projectTask' => 'ProjectTasksController' ]);
<form method="POST"action="/projectTask" class="box"> @csrf <div class="field"> <label class="label" for="description">New Task</label> <div class="control"> <input type="text" class="form-control" name="description" value=""> </div> </div> <br> <div class="field"> <div class="control"> <button type="submit" class="btn btn-primary">Add Task</button> </div> </div> </form>
Here is the error SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'project_id' cannot be null (SQL: insert into tasks (project_id, description, user_id, updated_at, created_at) values (, dadasdsadas, 1, 2020-01-11 17:59:32, 2020-01-11 17:59:32))
|
0

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]'; with protected $primaryKey ='task_id' in the Task model. It should be a string, not an array.

3 Comments

when we try this it gives us an error : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from tasks where id = 6 limit 1)
when we put in task_id it gives us this error in the browser, wheRequest URL: 127.0.0.1:8000/tasks/6 Request Method: POST Status Code: 302 Found Remote Address: 127.0.0.1:8000 Referrer Policy: no-referrer-when-downgraden we inspect it
Replace protected $primaryKey = [ 'task_id]'; with protected $primaryKey ='task_id'. It should be a string, not an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.