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 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B