douliang2935
2016-04-11 16:59
浏览 15
已采纳

如果选择其他单选按钮,则更新表中的特征行 - Laravel 5.2

I'am trying to make a featured photo for my product images. I have successfully made radio buttons with the product images id associated to them. When I click on a radio button and submit it, it sets a "featured" column in the product_images table to 1 ( or True). The problem I'm having is I only want 1 photo to be featured for each product. When I go choose and click a different image to be featured, it inserts it into the database with a 1 also.

How can I make it so there is only 1 featured image per product. So if some other image is selected, make all other images default back to 0 (or False) with that product_id.

Here is my 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>

My function to insert featured image in DB:

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

My product_images table: My Product Images Table

And my form: Upload form with select featured image form

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongmu2517 2016-04-11 18:19
    已采纳

    This should work, justy add some lines in ProductPhoto Model.

    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();
        }
    
    点赞 打赏 评论
  • doudu6100 2016-04-11 18:11

    try this

    <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();
            }
    
    点赞 打赏 评论
  • dongzhan2029 2016-04-12 05:45
    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();
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题