【问题标题】:Allow a user to bid on a product only once using laravel 5.1只允许用户使用 laravel 5.1 对产品出价一次
【发布时间】:2016-06-08 04:25:30
【问题描述】:

完成此操作所需的简单逻辑,我祈求专家的帮助。我目前正在尝试一种方法来对我正在开发的投标网站实施限制,简单的是:用户不能对产品进行多次投标。如果用户再次点击该产品,应该会提示他一个响应页面,指出用户之前曾对该产品出价,

这是刀片形式:

  <p><h3>{!! ucfirst($product->productname) !!}</h3></p>

            @if(Auth::user()->id === $product->user_id)
                <p>Sorry, you posted this product, you cannot quote on it.</p>

            @else
            <p>{!! Form::label('Higest Price') !!}</p>
            <p>{!! Form::number('price', Input::old('price')) !!}</p>
            <p>{!! Form::textarea('comments', Input::old('comments')) !!}</p>
            <p>{!! Form::hidden('product_id', $product->id) !!}</p>
            <p>{!! Form::hidden('company_id', $product->company_id) !!}</p>
            <p>{!! Form::hidden('user_id', $product->user_id) !!}</p>

            <p>{!! Form::submit('ADD QUOTE') !!}</p>
        @endif
        {!! Form::close() !!}

这是控制器:

public function store(BiddingCommentRequest $biddingCommentRequest)
    {
        $biddingComments = new BiddingComments;
        $product_id = $biddingCommentRequest->product_id;
        $AuthUserBidder = Auth::user()->id;
        $bidderCommented = BiddingComments::all();

        if($biddingCommentRequest->isMethod('post')){
            foreach ($bidderCommented as $key => $commentedBidder) {
               if(!count($commentedBidder->id) > 0)
               {
                    $biddingComments->bidder_id  = $AuthUserBidder;
                    $biddingComments->product_id = $product_id;
                    $biddingComments->company_id = $biddingCommentRequest->company_id;
                    $biddingComments->user_id    = $biddingCommentRequest->user_id;
                    $biddingComments->comments   = $biddingCommentRequest->comments;
                    $biddingComments->price      = $biddingCommentRequest->price;
                    $biddingComments->save();
                     return redirect()->route('biddingCommentView', $product_id)->with('message', 'Your question has been posted.');
               }elseif(($AuthUserBidder == $commentedBidder->bidder_id) && ($product_id == $commentedBidder->product_id))
               {
                    return redirect()->route('productindex')->with('message', 'Your question has been posted.');
               }else
               {
                    $biddingComments->bidder_id  = $AuthUserBidder;
                    $biddingComments->product_id = $product_id;
                    $biddingComments->company_id = $biddingCommentRequest->company_id;
                    $biddingComments->user_id    = $biddingCommentRequest->user_id;
                    $biddingComments->comments   = $biddingCommentRequest->comments;
                    $biddingComments->price      = $biddingCommentRequest->price;
                    $biddingComments->save();
                     return redirect()->route('biddingCommentView', $product_id)->with('message', 'Your question has been posted.');
                }
            }
        }
    }

它没有添加限制。它允许用户一遍又一遍地引用产品。请帮忙。

【问题讨论】:

    标签: if-statement eloquent laravel-5.1 logical-operators laravel-blade


    【解决方案1】:

    您的处理方法有很多问题,首先,我会在 Product 模型中选择 canQuote 方法,而不是直接在刀片中进行比较。

    function canQuote($user_id) {
        return static::where('user_id', $user_id)->where('id', $this->attributes['id'])->count() === 0;
    }
    

    接下来在 store 方法中,与其调用 all() 并检查一个可能在大型数据库中出错的循环,为什么不添加另一个方法来检查用户是否在利用 eloquent 的强大功能之前发表了评论

    【讨论】:

      猜你喜欢
      • 2015-03-12
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2014-02-11
      • 2014-11-15
      相关资源
      最近更新 更多