gpt4 book ai didi

php - 检索复选框以查看是否已选中 laravel 5.2

转载 作者:行者123 更新时间:2023-11-29 21:09:43 25 4
gpt4 key购买 nike

我的产品表单中有一个复选框,默认情况下该复选框未选中,并且设置为 0。如果选中该复选框,则会在数据库中插入 1 以使其知道产品将出现在首页。

我遇到的问题是,当我编辑产品时,即使数据库中有 1,该复选框也未被选中。我尝试了很多解决方案,但无法想出一个有效的解决方案。

这是我的功能:

public function addPostProduct(ProductRequest $request) {

// Check if checkbox is checked or not for featured product
$featured = Input::has('featured') ? true : false;

// Create the product in DB
$product = Product::create([
'product_name' => $request->input('product_name'),
'price' => $request->input('price'),
'cat_id' => $request->input('cat_id'),
'featured' => $featured
]);

// Save the product into the Database.
$product->save();

// Flash a success message
flash()->success('Success', 'Product created successfully!');

// Redirect back to Show all products page.
return redirect()->route('admin.product.show');
}

这是我的添加表单(仅显示复选框):

 <div class="form-group">
<label>Fetaured Product</label><br>
<input type="checkbox" name="featured" value="1">
</div>

这是我的编辑表单(仅显示复选框):

 <div class="form-group">
<label>Fetaured Product</label><br>
<input type="checkbox" name="featured" value="1">
</div>

我的表结构:

Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('product_name');
$table->decimal('price', 10, 2);
$table->integer('cat_id');
$table->integer('featured')->default(0);
$table->timestamps();
});

最佳答案

我猜我也必须更新我的 updateProduct 函数。

 public function updateProduct($id, ProductEditRequest $request) {

// Check if checkbox is checked or not for featured product
$featured = Input::has('featured') ? true : false;

$product = Product::where('id', '=', $id);

// Update your cart
$product->update(array(
'product_name' => $request->input('product_name'),
'price' => $request->input('price'),
'cat_id' => $request->input('cat_id'),
'featured' => $featured
));


// Find the Products ID from URL in route
$product = Product::findOrFail($id);

// Update the product with all the validation rules
$product->update($request->all());

// Flash a success message
flash()->success('Success', 'Product updated successfully!');

// Redirect back to Show all categories page.
return redirect()->route('admin.product.show');
}

我的编辑表单复选框如下:

<div class="form-group">
<label>Fetaured Product</label><br>
<input type="checkbox" name="featured" value="1" {{ $product->featured === 1 ? "checked=checked" : "" }}>
</div>

关于php - 检索复选框以查看是否已选中 laravel 5.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36488833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com