2016-04-07 13 views
0

私の製品フォームにチェックボックスがありますが、デフォルトでチェックボックスはチェックされていません。チェックボックスがオンの場合チェックされている場合は、1がデータベースに挿入され、製品がフロントページに掲載されることが通知されます。チェックボックスがチェックされているかどうか確認するlaravel 5.2

私が抱えている問題は、データベースに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(); 
     }); 
+3

チェックボックスは、魔法の理由だけで、あなたの中に価値が、自分自身をチェックしません。データベース。あなたがデフォルトでチェックされるようにするには 'checked'属性を出力する必要があります。 – CBroe

+0

このようなものがあります:** featured === 1? "checked = checked": ""}}> ** – David

+0

これはかなり一般的な方法です。 (これは[ブール値属性](https://www.w3.org/TR/html5/infrastructure.html#boolean-attribute)なので、少なくともHTML5では単純な 'checked'で十分ですが) – CBroe

答えて

0

私も、私は推測する私の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>