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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行