Why is Laravel trashed() method not found>

user1462432 picture user1462432 · May 14, 2014 · Viewed 7.5k times · Source

I'm trying to use the soft delete functionality of Elequent ORM in Laravel 4.1

Deleting records works as expected, however when I search for results using withTrashed() and then check to see if it was a soft deleted record using trashed() I get the following error

Call to undefined method Illuminate\Database\Eloquent\Collection::trashed()

Here is my code. Any suggestions?

$product = Product::withTrashed()->where('url', Input::get("product_url.$key"))->where('prolist_id', $list->id)->get();

if($product->trashed())
{
    $product->restore();
}

Answer

Joel Hinz picture Joel Hinz · May 14, 2014

get() is returning a collection of objects. If you only want one result, you can do first() instead and call trashed() on that. If you want several, you'll have to call the method individually for each item in a loop.