【发布时间】:2016-12-20 07:57:57
【问题描述】:
当时我对如何仅将一种产品标记为特色产品有点困惑。我在产品表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>
因此,我在下拉列表中显示所有产品,管理员可以从中选择另一个产品并将其标记为特色。已经在下拉列表中销售当前产品。
这是我放入控制器的内容
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\Exception\MethodNotAllowedHttpException
【问题讨论】: