简体   繁体   English

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

[英]Retrieving check-boxes to see if it is checked or not laravel 5.2

I have a check-box in my product form, by default the check-box is not checked, and is set to 0. if the check-box is checked, it will insert a 1 into database to let it know the product will be featured in the front page. 我的产品表单中有一个复选框,默认情况下,该复选框未选中,并且设置为0。如果选中该复选框,它将在数据库中插入1,以告知该产品在首页显示。

The problem I'm having is when I go edit the product, the check-box is not checked, even though there is a 1 in database. 我遇到的问题是,当我去编辑产品时,即使数据库中有1,也没有选中该复选框。 I have tried many solutions, but cant come up with a right one that will work. 我已经尝试了许多解决方案,但无法提出一种可行的解决方案。

Here is my function: 这是我的功能:

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');
    }

Here is my add form ( Just showing the check-box): 这是我的添加表单(仅显示复选框):

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

here is my edit form (just showing the check-box): 这是我的编辑表单(仅显示复选框):

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

And my table structure: 而我的表结构:

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();
        });

I had to update my updateProduct function as well I guess. 我猜我也必须更新我的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'); } 

And my edit form check-box with this: 我的编辑表单复选框与此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM