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

如果选择其他单选按钮,则更新表中的特征行 - 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();
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀