我正在尝试为 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!');
}
添加->first()
方法调用:
$product = Product::where('product_id', $product_featured_id)->first();
在这种情况下,您将获得一个对象,而不是查询生成器的实例。