예제 #1
0
 protected function buy($slug)
 {
     //Get buyer
     $auction = Auction::where('slug', $slug)->first();
     $buyer = User::findOrFail(Auth::user()->id);
     $seller = User::findOrFail($auction->seller_id);
     // Make sure users can't buy own works
     if ($buyer->id === $seller->id) {
         return FALSE;
     }
     //Get other bidders
     $users = DB::table('users')->select(DB::raw('users.id'))->join('bids', 'users.id', '=', 'bids.bidder_id')->where('bids.auction_id', $auction->id)->where('users.id', '!=', Auth::user()->id)->get();
     //E-mail buyer
     Message::auctionBought($auction->id, Auth::user()->id);
     //E-mail other bidders
     if (count($users)) {
         foreach ($users as $user) {
             Message::auctionClosed($auction->id, $user->id);
         }
     }
     //E-mail seller
     Message::auctionSold($auction->id, $buyer->id);
     //Remove from view
     $buying = Auction::find($auction->id);
     $buying->state = "sold";
     $buying->save();
     return $auction;
 }