如何禁止接受已经接受的汽车报价Laravel

时间:2019-03-22 09:35:40

标签: mysql laravel eloquent

我尝试详细解释代码。卖方收到了更多关于他的汽车的报价。当他接受某辆汽车的特定要约时,我要禁止他再次接受同一辆汽车的其他要约。 例。卖方接受了要约编号。 2,价格3000,car_id 4。 卖家不再接受要约编号3、4或5,价格4000,car_id 4。 看我的代码和数据库

   public function accept($id){
        $userId =  Auth::user();
        $offerId = Offer::with('user')->find($id);
        if(!$offerId){
            return response()->json([
                'message' => 'Oooops this offer does not exist or is currently unavailable'
            ]);
        }
        if($offerId->accepted === 1){
            return response()->json([
                'message' => 'Oooops this offer is already accepted'
            ]);
        }

     $res =   DB::table('offers')
            ->where('id', $id)
            ->update(['accepted' => 1]);
     if(!$res){
         return response()->json([
             'message' => 'Ooooops the offer is already accepted'
         ]);
     }
        Mail::to($offerId->user->email)->send(new AcceptOffer());
        return response()->json([
            'message' => 'Successfully accepted offer',
            'offer' => $id
        ]);

    }

我的餐桌要约与要约一起操纵:

https://imgur.com/ccs4abV

要约与汽车和用户之间的关系:

汽车:

public function user() {
    return $this->belongsTo('App\User');
}
public function offer() {
    return $this->hasMany('App\Offer');
}

优惠:

public function car() {
    return $this->belongsTo('App\Car');
}
public function user() {
    return $this->belongsTo('App\User');
}

用户:

public function cars(){
    return $this->hasMany('App\Car');
}
public function offers(){
    return $this->hasMany('App\Offer');
}

1 个答案:

答案 0 :(得分:1)

简单检查一下,是否已经接受了该特定汽车的报价。

$acceptedCount = Offer::where('car_id', $offerId->car_id)->where('accepted', 1)->count(); 
if($acceptedCount > 0){
    return response()->json([
         'message' => 'Another offer for this car was already accepted'
     ]);
}