3👍
✅
The update()
function returns an integer indicating whether or not the update (which is a bulk update, because Course::whereId($id)
returns a collection of results, not a single one).
This should work:
$course = Course::findOrFail($id);
$course->update([
'name' => $request->name,
'title' => $request->title,
'description' => $request->description,
'is_active' => $request->is_active
]);
0👍
The update()
method is going to return a boolean
value so $course
is not going to be the model you want to update. As for why the error is saying integer
instead of boolean
I’m not 100% sure.
There are a couple of solutions to get the corse:
-
Just use
find()
orfindOrFail()
$course = Course::findOrFail($id);
-
public function update(Request $request, Course $course)
Going with option 1 as an example your code would look something like:
public function update(Request $request, $id)
{
$request->validate([
'name' => 'required',
'title' => 'required',
]);
$course = Course::findOrFail($id);
$course->update([
'name' => $request->name,
'title' => $request->title,
'description' => $request->description,
'is_active' => $request->is_active,
]);
$course->programs()->sync($request->input('programs', []));
return response()->json([
'course' => $course,
'message' => 'Course has been Updated!',
]);
}
In the above example I have used sync() instead of detaching and the attaching the new ids.
👤Rwd
Source:stackexchange.com