/** * @param $amount - how many locations to pick * @param $exclude - array of locations to exclude * @return mixed */ public static function pickUnused($amount, $exclude = []) { $used = array_keys(Location::has('targets')->get()->keyBy('id')->toArray()); foreach ($exclude as $location) { $used[] = $location->id; } $available = Location::whereNotIn('id', $used)->get(); $locations = $available->random($amount); return $locations; }
/** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $total = Location::all()->count(); $used = Location::has('targets')->count(); $free = $total - $used; if ($free < 5) { return redirect('/locations'); } $location = Location::pickUnused(1); return view('experiments.create', compact('location', 'free')); }
/** * @param $id * @param bool $withUsers * @return mixed * @throws GeneralException */ public function findOrThrowException($id, $hasPcode = false) { if (empty($id)) { throw new GeneralException("Location id is empty."); } if ($hasPcode) { $location = Location::has('pcode')->find($id); } else { $location = Location::find($id); } if (!is_null($location)) { return $location; } throw new GeneralException("That location with id {$id} does not exist."); }