Reputation: 354
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
Upvotes: 2
Views: 3268
Reputation: 111899
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}
Upvotes: 1
Reputation: 778
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!
Upvotes: 0
Reputation: 3452
$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.
Upvotes: 1