Created
June 24, 2016 06:25
-
-
Save Tjoosten/2aa6c5ebf96f6cf7733dcd80e724444f 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\Controllers; | |
use App\Departments; | |
use App\User; | |
use Illuminate\Http\Request; | |
use App\Http\Requests; | |
class DepartmentController extends Controller | |
{ | |
/** | |
* DepartmentController constructor. | |
*/ | |
public function __construct() | |
{ | |
$this->middleware('auth'); | |
$this->middleware('lang'); | |
} | |
/** | |
* Get all the departments. | |
* | |
* @url GET: /departments | |
* @param Request $request | |
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View | |
*/ | |
public function index(Request $request) | |
{ | |
$rels = ['users', 'managers']; | |
$term = $request->get('term'); | |
if (empty($term)) { | |
$data['departments'] = Departments::with($rels)->paginate(15); | |
} else { | |
$data['departments'] = Departments::with($rels) | |
->where('name', 'LIKE', "%$term%") | |
->paginate(15); | |
} | |
return view('departments.index', $data); | |
} | |
/** | |
* Create form for registering new departments | |
* | |
* @url GET: /departments/create | |
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View | |
*/ | |
public function register() | |
{ | |
dd('test'); | |
$data['users'] = User::all(['id', 'name']); | |
return view('departments.create', $data); | |
} | |
/** | |
* Create a new department. | |
* | |
* @url POST: /departments/create | |
* @param Requests\DepartmentValidator $input | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
public function create(Requests\DepartmentValidator $input) | |
{ | |
$depId = Departments::create($input->except('_token'))->id; | |
$department = Departments::findOrFail($depId); | |
$department->users()->attach($input->users); | |
$department->managers()->attach($input->managers); | |
session()->flash('message', 'Department has been created'); | |
return redirect()->back(); | |
} | |
/** | |
* Show The information about a specific department. | |
* | |
* @url /departments/{id} | |
* @param int $id The department id in the database. | |
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View | |
*/ | |
public function show($id) | |
{ | |
$data['department'] = Departments::find($id); | |
return view('departments.show', $data); | |
} | |
/** | |
* Update a department in the database. | |
* | |
* @url POST: /departments/update/{id} | |
* @param Requests\DepartmentValidator $input | |
* @param int $id The department id in the database. | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
public function update(Requests\DepartmentValidator $input, $id) | |
{ | |
Departments::find($id)->update($input->except('_token')); | |
session()->flash('message', 'Department has been updated.'); | |
return redirect()->back(); | |
} | |
/** | |
* Destroy a department in the database. | |
* | |
* @url GET: /departments/delete/{id} | |
* @param int $id The department identifier in the database. | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
public function destroy($id) | |
{ | |
$department = Departments::findOrFail($id); | |
$department->users()->sync([]); | |
$department->managers()->sync([]); | |
$department->delete(); | |
session()->flash('message', 'Department deleted'); | |
return redirect()->back(); | |
} | |
} |
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
url http://localhost:8000/departments/new returns white page. |
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 | |
// Department routes. | |
Route::get('departments', 'DepartmentController@index')->name('department.index'); | |
Route::get('/departments/search', 'DepartmentController@index')->name('department.search'); | |
Route::get('/departments/{id}', 'DepartmentController@show')->name('department.specific'); | |
Route::get('/departments/create', 'DepartmentController@register')->name('department.register'); | |
Route::get('/departments/delete/{id}', 'DepartmentController@destroy')->name('department.destroy'); | |
Route::post('/departments/create', 'DepartmentController@create')->name('department.store'); | |
Route::post('/departments/update/{id}', 'DepartmentController@update')->name('department.update'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment