我试图在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!');
}
答案 0 :(得分:1)
添加->first()
方法调用:
$product = Product::where('product_id', $product_featured_id)->first();
在这种情况下,您将获得一个对象而不是Query Builder的实例。