将产品设置为 laravel 中的特色
Set product as featured in laravel
我对如何同时仅将一种产品标记为特色产品有些困惑。我在我的产品 table featured
中添加了列,它接受 0
用于普通产品,1
用于特色产品。
只能有一种产品具有 1
的特色
所以我在 blade 下面的下拉列表中显示了所有产品
{{ 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>
像这样,我在下拉列表中显示了所有产品,管理员可以从中 select 另一个产品并将其标记为精选产品。已在下拉列表中销售当前产品。
这是我放入控制器的内容
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']);
我如何才能在控制器中创建逻辑,以便将我在下拉列表中 select 的产品更新为 1
并在数据库中更新为 0
?
目前错误是
production.ERROR: Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
您的表单应打开为:
{{ 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)) }}
但我觉得这里少了点什么
我对如何同时仅将一种产品标记为特色产品有些困惑。我在我的产品 table featured
中添加了列,它接受 0
用于普通产品,1
用于特色产品。
只能有一种产品具有 1
所以我在 blade 下面的下拉列表中显示了所有产品
{{ 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>
像这样,我在下拉列表中显示了所有产品,管理员可以从中 select 另一个产品并将其标记为精选产品。已在下拉列表中销售当前产品。
这是我放入控制器的内容
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']);
我如何才能在控制器中创建逻辑,以便将我在下拉列表中 select 的产品更新为 1
并在数据库中更新为 0
?
目前错误是
production.ERROR: Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
您的表单应打开为:
{{ 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)) }}
但我觉得这里少了点什么