将产品设置为Laravel中的特色



我在当时仅标记一个产品,我有点卡住。我在产品表中添加了列featured,该列接受了普通产品的0,而1则为特色。

1

只有一种产品

所以我在下拉下放置了刀片,显示所有产品

{{ Form::open() }}
<div class="form-group">
    <label for="title" class="control-block">Assign Product as Featured:</label>
        <select class="form-control" name="featured">       
            @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() }}
<p>Current Featured Product: <strong>@if($featured->featured == 1){{ $featured->title }}@endif</strong></p> 

所以像这样,我在下拉中显示了所有产品,从admin可以选择另一个产品并标记为特色。

已经在下拉销售当前产品。

这就是我放在控制器中的内容

public function products() {
    $products = Product::all();
    return View::make('site.admin.products', [
        'products' => $products
    ]);
}
public function featuredProduct($productId) {
    $product = Product::where('product_id', $productId)->first();
    if (!$product) {
        App::abort(404);
    }
    $product_featured = Input::get('featured', $product->featured);
    $product->featured = $product_featured;
    $product->save();
    return Redirect::to('/admin/products');
}

和路线

Route::get ('/admin/products', ['uses' => 'AdminController@products', 'before' => 'admin']);
Route::post ('/admin/products/{productId}', ['uses' => 'AdminController@featuredProduct', 'before' => 'admin']);

我如何确切地在控制器中制作逻辑,因此要更新我在下拉列表中选择1的产品,并在数据库中最新到0

当前错误是

production.Error:Symfony Component httpkernel extception Methodnotloweredhttpexception

您的表格应以:

打开
{{ Form::open(['url' => '/admin/products/feature', 'method' => 'post']) }}

,您的路线应为:

Route::post('/admin/products/feature', ['uses' => 'AdminController@featuredProduct', 'before' => 'admin']);

您可以写下逻辑为:

public function featuredProduct() {
    $product_featured_id = Input::get('featured');
    $product = Product::where('product_id', $product_featured_id)->firstOrFail();
    Product::where('featured', 1)->update(['featured' => 0]); // this will make all product of featured 0
    $product->featured = 1;
    $product->save();
    return Redirect::to('/admin/products');
}

您需要设置表单操作

{{ Form::open(array('url' => '/admin/products/' . $product_id)) }}

,但我觉得这里缺少某些东西

相关内容

  • 没有找到相关文章

最新更新