Skip to content

Instantly share code, notes, and snippets.

@madeinnordeste
Created January 26, 2017 17:05
Show Gist options
  • Save madeinnordeste/21650f2a5a82c76dd9bab70d60c5ecd3 to your computer and use it in GitHub Desktop.
Save madeinnordeste/21650f2a5a82c76dd9bab70d60c5ecd3 to your computer and use it in GitHub Desktop.
Laravel 5.3 Resource Controller Example
<?php
namespace App\Http\Controllers;
use App\State;
use App\Patient;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Repositories\PatientRepository;
class PatientController extends Controller
{
/**
* The patient repository instance.
*
* @var PatientRepository
*/
protected $patients;
/**
* Create a new controller instance.
*
* @param PatientRepository $patients
* @return void
*/
public function __construct(PatientRepository $patients)
{
$this->patients = $patients;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$search = Input::get('search', NULL);
$patients = $this->patients->paginate(20, $search);
//debug($patients);
$data = ['objects' => $patients, 'search' => $search];
//return view('patient.index', $data);
return view('patient.index-crudlist', $data);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$patient = new Patient();
$states = State::pluck('name', 'id');
$data = ['patient' => $patient, 'states' => $states];
return view('patient.form')->with($data);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validator = $this->patients->validator(Input::all());
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}else{
$patient = new Patient();
$patient->fill(Input::all())->save();
return redirect()
->route('patients.edit', ['id' => $patient->id])
->with('success', 'Dados salvos com sucesso.');
//return back()->with('success', 'Dados salvos com sucesso.');
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Patient $patient)
{
return redirect()
->route('patients.edit', ['id' => $patient->id]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Patient $patient)
{
if( is_null($patient) ){
return redirect('patients')->withErrors('Paciente não encontrado');
}
$states = State::pluck('name', 'id');
$data = ['patient' => $patient, 'states' => $states];
return view('patient.form')->with($data);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Patient $patient)
{
$validator = $this->patients->validator(Input::all());
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}else{
$patient->fill(Input::all())->save();
return back()->with('success', 'Dados atualizados com sucesso.');
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Patient $patient)
{
if( is_null($patient) ){
return redirect('patients')->withErrors('Paciente não encontrado');
}
$patient->delete();
return back()->with('success', 'Paciente removido sucesso.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment