/** * Store a newly created resource in storage. * * @return Response */ public function store($id) { $input = Request::all(); $number = $input['envelope_number']; if (!empty($number)) { Validator::extend('envelope_number_fail', function ($attr, $value, $params) { $exists = Donor::where(array('organisation_id' => $params[0], 'envelope_number' => Input::get('envelope_number')))->first(); if ($exists) { // true is false in this case. We want to fail if items does exist return false; } return true; }); $val = Validator::make($input, ['envelope_number' => 'envelope_number_fail:' . $id]); if ($val->fails()) { return Redirect::to('/dashboard/' . $id . '/donor/create/')->withErrors($val); } $input['organisation_id'] = $id; $input['envelope_number'] = $number; $donor = Donor::create($input); } else { DB::beginTransaction(); try { $latestEnvelope = Donor::where(array('organisation_id' => $id))->orderBy('envelope_number', 'desc')->first(); $input['organisation_id'] = $id; $input['envelope_number'] = $latestEnvelope->envelope_number + 1; $donor = Donor::create($input); DB::commit(); } catch (Exception $e) { DB::rollback(); } } return redirect('/dashboard/' . $id . '/donor/view/' . $donor->id); }
public function filterDonors(Request $request) { // if ($request->blood === "All") { $donors_by_blood = Donor::all()->toArray(); } else { $blood_id = \App\Blood::where("blood", $request->blood)->first(); $blood_id = $blood_id->id; $donors_by_blood = Donor::where("blood_id", $blood_id)->get(); } $final_donors = []; if ($request->governorate === "All") { $final_donors = $donors_by_blood; } else { $gov_id = \App\Governorate::where("governorate", $request->governorate)->first(); $gov_id = $gov_id->id; foreach ($donors_by_blood as $donor) { if ($donor['governorate_id'] === $gov_id) { array_push($final_donors, $donor); } } } for ($i = 0; $i < count($final_donors); $i++) { $blood_type = \App\Blood::find($final_donors[$i]['blood_id'])->blood; $final_donors[$i]["blood_type"] = $blood_type; $governorate = \App\Governorate::find($final_donors[$i]['governorate_id'])->governorate; $final_donors[$i]["governorate"] = $governorate; } // dd($final_donors); return response()->json($final_donors); }