完成这项工作所需的简单逻辑,我祈求专家的帮助。我现在正试图在我正在开发的竞价网站上实施限制,简单就是:用户不能多次竞价产品。如果用户再次点击该产品,系统会提示他提供一个回复页面,告知用户之前已对该产品进行过出价,
这是刀片形式:
<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.');
}
}
}
}
它没有添加限制。它允许用户一遍又一遍地继续引用产品。请帮助。
答案 0 :(得分:0)
您的方法有很多问题,首先,我会在产品型号中选择canQuote
方法,而不是直接在刀片中进行比较。
function canQuote($user_id) {
return static::where('user_id', $user_id)->where('id', $this->attributes['id'])->count() === 0;
}
接下来在store方法中,不是调用all()
并检查一个大型数据库可能出错的循环,为什么不在利用eloquent的力量之前添加另一个检查用户是否已注释的方法< / p>