/** * Creates the Ticket::hasCode($code) scope for locating tickets in the db * using their QR codes. Just use Ticket::hasCode($code)->first() to get the * ticket object in question! * * @param Query $query The Laravel Query object that we operate here. * @param String $code Encoded, encrypted code string. * * @return Query Query focued on the ticket referenced in the string. */ public function scopeHasCode($query, $code) { // Get the ticket details from our code $decoded = json_decode(Crypt::decrypt($code)); // Search for them in the database. return $query->where('id', $decoded->id)->where("user_id", $decoded->user_id)->where("event_id", $decoded->event_id); }