Я пытаюсь создать функцию для рекомендуемого/не рекомендуемого продукта в Laravel 4.2, но получаю сообщение об ошибке
Вызов неопределенного метода Illuminate\Database\Query\Builder::save()
Это моя функция контроллера
public function featuredProduct() {
$product_featured_id = Input::get('featured');
$product = Product::where('product_id', $product_featured_id);
Product::where('featured', 1)->update(['featured' => 0]); // make all other products featured -> 0
$product->featured = 1;
$product->save();
return Redirect::to('/admin/products')->with('message', 'Product marked as featured!');
}
И это форма в представлении
{{ Form::open(['url' => '/admin/products/feature', 'method' => 'post']) }}
<div class="form-group">
<select class="form-control" name="featured">
<option value="0">Remove from Featured</option>
@foreach($products as $featured)
<option value="{{ $featured->product_id }}" {{ $featured->featured == 1 ? "selected" : ""}}>{{ $featured->title }}</option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Make Product Featured</button>
{{ Form::close() }}
В таблице с продуктами у меня есть столбец, который сохраняет 1
для рекомендуемого продукта и 0
для нерекомендуемого продукта.
Что мне здесь не хватает?
Обновить:
public function featuredProduct() {
$product_featured_id = Input::get('featured');
if($product_featured_id == 0)
{
Product::where('featured', 1)->update(['featured' => 0]); // make all other products featured -> 0
}
else
{
$product = Product::where('product_id', $product_featured_id)->first();
Product::where('featured', 1)->update(['featured' => 0]); // make all other products featured -> 0
$product->featured = 1;
$product->save();
}
return Redirect::to('/admin/products')->with('message', 'Product marked as featured!');
}