2016-04-11 1 views
0

Ich versuche, ein vorgestellten Foto für meine Produktbilder zu machen. Ich habe erfolgreich Radioknöpfe mit der Produktbildidentifikation hergestellt, die mit ihnen verbunden ist. Wenn ich auf ein Optionsfeld klicke und es absende, wird eine "featured" Spalte in der Tabelle product_images auf 1 (oder True) gesetzt. Das Problem, das ich habe, ist, ich möchte nur 1 Foto für jedes Produkt vorgestellt werden. Wenn ich gehe, wählen Sie und klicken Sie auf ein anderes Bild, um gekennzeichnet zu werden, es fügt es in die Datenbank mit einer 1 auch ein.Update vorgestellten Zeile in der Tabelle, wenn andere Radio-Button gewählt - Laravel 5.2

Wie kann ich es so machen, es gibt nur 1 vorgestellten Bild pro Produkt. Wenn also ein anderes Bild ausgewählt wird, stellen Sie alle anderen Bilder mit dieser Produkt-ID auf 0 (oder Falsch) zurück.

Hier ist meine Form:

public function storeFeaturedPhoto($id, Request $request) { 

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

     // Validate featured button 
     $this->validate($request, [ 
      'featured' => 'required' 
     ]); 

     // Grab the ID of the Image being featured from radio button 
     $featured2 = Input::get('featured'); 

     // Select All from product_images where the photo ID is the photo id being selected from radio button 
     $image = ProductPhoto::where('id', '=', $featured2); 


     // Select from product_images wherew featured = 1 and the product ID = to the product id in the URL, then count the results 
     $count = ProductPhoto::where('featured', '=', 1)->where('product_id', '=', $id)->count(); 

     if ($count > 1) { 
      // Do something here to deselect all other images with this product_id?????????? 
     } 

     // update the product_photos table with the featured photo 
     $image->update([ 
      'featured' => $featured, 
     ]); 

     // Return redirect back 
     return redirect()->back(); 
    } 

Meine product_images Tabelle: My Product Images Table

Und meine Form

  <form method="post" action="/store/admin/products/add/featured/{{ $products->id }}"> 
       {!! csrf_field() !!} 

       @foreach($products->photos as $set) 
        <label>{{ $set->id }}&nbsp;&nbsp;&nbsp;</label> 
        <input type="radio" name="featured" value="{{ $set->id }}" {{ $set->featured === 1 ? "checked=checked" : "" }}>      
       @endforeach 


       <button type="submit" class="btn btn-primary waves-effect waves-light">Feature Image</button> 


      </form> 

Meine Funktion Ausgewähltes Bild in DB einzufügen: Upload form with select featured image form

Antwort

1

Diese Arbeit sollte justy einige Zeilen in Product Modell hinzufügen.

class ProductPhoto extends Model 
{ 
    protected $fillable = ['featured']; 

    //other things... 
} 

Controller:

public function storeFeaturedPhoto($id, Request $request) { 
     // Validate featured button 
     $this->validate($request, [ 
      'featured' => 'required|exists:product_images,id' 
     ]); 

     // Grab the ID of the Image being featured from radio button 
     $featured = Input::get('featured'); 

     // Some mass updates 
     ProductPhoto::where('product_id', '=', $id)->update(['featured' => 0]); 
     ProductPhoto::findOrFail($featured)->update(['featured' => 1]); 


     // Return redirect back 
     return redirect()->back(); 
    } 
+0

Ja, das funktioniert. Danke. – David

0

versuchen dieses

<form method="post" action="/store/admin/products/add/featured/{{ $products->id }}"> 
       {!! csrf_field() !!} 

       @foreach($products->photos as $set) 
        <label>{{ $set->id }}&nbsp;&nbsp;&nbsp;</label> 
        <input type="radio" name="featured" value="{{ $set->id }}">      
       @endforeach 


       <button type="submit" class="btn btn-primary waves-effect waves-light">Feature Image</button> 


      </form> 

Controller

public function storeFeaturedPhoto($id, Request $request) { 

      // Check if radio button is checked or not for featured image 
      // Grab the ID of the Image being featured from radio button 

      $featured = $request->input('featured'); 


      // Validate featured button 
      $this->validate($request, [ 
       'featured' => 'required' 
      ]); 



      // Select All from product_images where the photo ID is the photo id being selected from radio button 
      $image = ProductPhoto::findOrfail($featured); 
//radio buttom! u lust have only one selected thats how it works i think (in groups) use check box and exlain this part more 


      // Select from product_images wherew featured = 1 and the product ID = to the product id in the URL, then count the results 

      $count = ProductPhoto::where('featured', '=', 1)->where('product_id', '=', $id)->get(); 

      if ($count > 1) { 
       foreach ($count as $prod) 
    { 
     $prod->featured = 0; 
     $prod->save(); 
     } 
      } 

      // update the product_photos table with the featured photo 
      $image->update([ 
       'featured' => $featured, 
      ]); 

      // Return redirect back 
      return redirect()->back(); 
     } 
0
public function storeFeaturedPhoto($id, Request $request) { 
    $this->validate($request, [ 
    'featured' => 'required|exists:product_images,id' 
    ]); 

    $featured = Input::get('featured'); 
    if($featured==1){ 
     ProductPhoto::where(['featured'=>1])->update(['featured' => 0]); 
    } 
    ProductPhoto::where('product_id', '=', $id)->update(['featured' => 1]); 
    // Return redirect back 
    return redirect()->back(); 
}