Last active
April 6, 2018 16:35
-
-
Save blegoh/6b39cdd36738c4c2d5886b5857ad04a9 to your computer and use it in GitHub Desktop.
Trait for CRUD Resource Controller
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\Traits; | |
use Illuminate\Http\Request; | |
use Yajra\DataTables\DataTables; | |
trait Resource | |
{ | |
/** | |
* Display a listing of the resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index() | |
{ | |
return view($this->view . '.index'); | |
} | |
/** | |
* Show the form for creating a new resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function create() | |
{ | |
return view($this->view . '.create'); | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request) | |
{ | |
$this->validate($request, $this->model::$rulesCreate); | |
$this->model::create($request->all()); | |
return redirect(route($this->route . '.index')); | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function show($id) | |
{ | |
// | |
} | |
/** | |
* Show the form for editing the specified resource. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function edit($id) | |
{ | |
$data = $this->model::find($id); | |
return view($this->view . '.edit', compact('data')); | |
} | |
/** | |
* Update the specified resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function update(Request $request, $id) | |
{ | |
$data = $this->model::find($id); | |
$this->validate($request, $this->model::rulesEdit($data)); | |
$data->update($request->all()); | |
return redirect(route($this->route.'.index')); | |
} | |
/** | |
* Remove the specified resource from storage. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function destroy($id) | |
{ | |
$this->model::destroy($id); | |
} | |
/** | |
* Process datatables ajax request. | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
public function anyData() | |
{ | |
return Datatables::of($this->model::query()) | |
->addColumn('action', function ($data) { | |
$del = '<a href="#" data-id="' . $data->id . '" class="btn btn-danger hapus-data">Delete</a>'; | |
$edit = '<a href="' . route($this->route . '.edit', [$this->route => $data->id]) . '" class="btn btn-primary">Edit</a>'; | |
return $edit . ' ' . $del; | |
}) | |
->make(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment