Last active
March 5, 2019 09:35
-
-
Save jakewtaylor/0cfe9671e07942e989354695a13c287b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Requests; | |
use Illuminate\Foundation\Http\FormRequest; | |
use App\Models\Patient; | |
use App\Models\Practice; | |
use App\Models\Lab; | |
class CreateLabOrderRequest extends FormRequest | |
{ | |
public function authorize() | |
{ | |
return true; | |
} | |
public function rules() | |
{ | |
return [ | |
'patient_id' => 'integer|required', | |
'practice_id' => 'integer|required', | |
'lab_id' => 'integer|required', | |
'lens' => 'string|required', | |
'reference' => 'string|required', | |
'date_sent' => 'date|required', | |
'date_required' => 'date|required', | |
]; | |
} | |
public function getPatient() | |
{ | |
$patientId = $this->input('patient_id'); | |
return Patient::find($patientId); | |
} | |
public function getPractice() | |
{ | |
$practiceId = $this->input('practice_id'); | |
return Practice::find($practiceId); | |
} | |
public function getLab() | |
{ | |
$labId = $this->input('lab_id'); | |
return Lab::find($labId); | |
} | |
public function getLabOrderData() | |
{ | |
return $this->only('lens', 'reference', 'date_sent', 'date_required'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Api; | |
use App\Http\Controllers\Controller; | |
use App\Http\Requests\CreateLabOrderRequest; | |
use App\Http\Resources\LabOrder as LabOrderResource; | |
use App\Http\Models\LabOrder; | |
class LabOrdersController extends Controller | |
{ | |
// ... | |
public function store(CreateLabOrderRequest $request) | |
{ | |
$patient = $request->getPatient(); | |
$practice = $request->getPractice(); | |
$lab = $request->getLab(); | |
$labOrderData = $request->getLabOrderData(); | |
$labOrder = new LabOrder($labOrderData); | |
$labOrder->patient()->associate($patient); | |
$labOrder->practice()->associate($practice); | |
$labOrder->lab()->associate($lab); | |
$labOrder->save(); | |
/* | |
Lines 22-26 could be changed to this: | |
$labOrder = tap(new LabOrder($labOrderData), function ($instance) use ($patient, $practice, $lab) { | |
$instance->patient()->associate($patient); | |
$instance->practice()->associate($practice); | |
$instance->lab()->associate($lab); | |
})->save(); | |
*/ | |
return LabOrderResource::make($labOrder); | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment