2

I want to change the status of a task to complete. I have a status_id column in the database and 1 equals complete. I would like the click of the button to change the status_id to 1

My route

Route::patch('/tasks/completed/{Task}', 'TasksController@completedUpdate')->name('completedUpdate');

My button

<form action="{{ route('completedUpdate', $task->id) }}" method="POST">
    {{ csrf_field() }}
    {{ method_field('PATCH') }}
    <button type="submit" class="button is-inverted" style="margin-top: 10px;">Mark Complete</button>
  </form>

My controller

public function completedUpdate(Request $request, $task)
{
    $task->status_id = $request->status_id;

    $task->save;

    return redirect()->back()->with('message', 'task marked complete');
}

the error it gives me is:

Attempt to assign property of non-object

Let me know if any more info is needed

3 Answers 3

1

You should change:

public function completedUpdate(Request $request, $task)
{
    $task->status_id = $request->status_id;

    $task->save;

    return redirect()->back()->with('message', 'task marked complete');
}

into:

public function completedUpdate(Request $request, Task $task)
{
    $task->status_id = $request->status_id;

    $task->save();

    return redirect()->back()->with('message', 'task marked complete');
}

so you need to typehint type of $task variable and use save() method instead of save property.

Also probably instead of:

/tasks/completed/{Task}

you should use:

/tasks/completed/{task}
Sign up to request clarification or add additional context in comments.

2 Comments

ok thank you! how does the controller know to change the status_id to 1
This is done in this line $task->status_id = $request->status_id; - you send 1 in $request->status_id so it's set to 1 and in next line it's saved
1

$task->save; should be $task->save();

With ->save, it is looking for a property on the model, hence the error message re 'assigning a property'. Whereas ->save() calls the save method on the object.

1 Comment

Thank you! I will remember that about save
0

In your controller, you're assigning the $task->status_id a value of $request->status_id but you're actually not passing the status_id in your form in your HTML code. You can put a hidden element in your form which is <input name="status_id" value="1" />.

In the meanwhile, do not forget that $task->save; must be $task->save();

Good luck!

2 Comments

ok thank you! I have added that input to the form and hid it but do I need to add anything to the controller?
@L.Fox No you won't. You just need to explicit it somewhere in your controller that you want to set the column to 1, you can hard-code it or use a hidden input in the submitted form.

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.