Nessuna descrizione

SetDept.php 876B

1234567891011121314151617181920212223242526272829
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. class SetDept
  5. {
  6. /**
  7. * Sets the department session variable to the desired department id or the default if none is set.
  8. *
  9. * @param \Illuminate\Http\Request $request
  10. * @param \Closure $next
  11. * @return mixed
  12. */
  13. public function handle($request, Closure $next)
  14. {
  15. if ($request->has('setdept')) {
  16. $data = $request->validate(['setdept' => 'integer|exists:departments,id']);
  17. $request->session()->put('department', $data['setdept']);
  18. }
  19. if (!$request->session()->has('department')) {
  20. $default_dept_id = \App\Department::whereNull('name')->first()->id;
  21. $request->session()->put('department', $default_dept_id);
  22. }
  23. // dump($request->session()->get('department'));
  24. return $next($request);
  25. }
  26. }