2016-04-07 16 views
0

Ürün formumda bir onay kutum var, varsayılan olarak onay kutusu işaretli değil ve onay kutusu 0 ise işaretliyse, ürünün ön sayfada yer alacağını bildirmek için veritabanına 1 ekleyecektir.Kontrol edilip edilmediğini görmek için onay kutularını çağırma veya alma olup olmadığını görmek için 5.2

Yapmakta olduğum sorun, ürünü düzenlediğimde, veritabanında 1 olsa bile onay kutusu işaretli değil. Birçok çözüm denedim, ancak işe yarayacak doğru biriyle gelemiyorum.

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

(sadece gösteriliyor düzenleme biçimidir: Burada

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

benim eklenti şeklidir (Sadece onay kutusunu gösteren): Burada

benim işlevidir onay kutusu):

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

Ve tablo yapısı:

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

Onay kutuları sihirli sırf bir değerin, kendilerini kontrol etmiyoruz senin veri tabanı. Varsayılan olarak işaretlenmesini istiyorsanız, 'checked' özniteliğini çıkarmanız gerekir. – CBroe

+0

böylece böyle bir şey: ** featured === 1? "checked = checked": ""}}> ** – David

+0

Evet, bunu yapmanın oldukça yaygın bir yolu. (Basit bir 'kontrol' yeterli olsa da, en azından HTML5'te, bu bir [boole özniteliği] olduğundan (https://www.w3.org/TR/html5/infrastructure.html#boolean-attribute).) – CBroe

cevap

0

Ben de sanırım benim updateProduct fonksiyonunu güncellemek zorunda kaldı. Bununla

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

Ve düzenleme formlarındaki işaret kutusu:

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

İlgili konular