我有点不知道如何在当时只标记一种产品。我在我的产品表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
答案 0 :(得分:1)
您的表单应按以下方式打开:
{{ 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');
}
答案 1 :(得分:1)
您需要设置表单操作
{{ Form::open(array('url' => '/admin/products/' . $product_id)) }}
但我觉得这里缺少了一些东西