使产品 featured/not 成为 Laravel 的特色

Make product featured/not featured with Laravel

我正在尝试为 Laravel 4.2 中的 featured/not 特色产品创建功能,但出现错误

Call to undefined method 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() }}

在包含产品的 table 列中,我为特色产品保存了 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();

在这种情况下,您将获得一个对象而不是查询生成器的实例。