1
votes

When I try to update a model with this code:

public function updateMixedtape($slug, Request $request)
{
    $mix = Mix::where('slug', $slug)->get();
    $mix->update($request->all());
    return redirect('dashboard/mixes');
}

I get an error that method update doesn't exist. However if I modify my view to send a radio_show_id instead of slug and try to change the code to something like this:

public function updateMixedtape(Request $request)
{
    $mix = Mix::findOrFail($request->radio_show_id);
    $mix->update($request->all());
    return redirect('dashboard/mixes');
}

The code executes without any errors.

What puzzles me is that if I do something like return $mix; before the line where I call the update method, I get similar data for both methods.

1
with get() a Collection() is returned. Not a model. Just use first() instead of get() in your case - shock_gone_wild
Thanks.. It worked.. - Maxson Ndlovu

1 Answers

0
votes

As shock_gone_wild has suggested in the comment section of my question $mix = Mix::where('slug', $slug)->get(); is returning a collection and not a model. This is because a Model::where() method can return zero, one or many records depending on whether or not there are records that meet the set condition.

As suggested I used $mix = Mix::where('slug', $slug)->first(); instead to get the first record that meets the condition.