As I understand you need an admin system, if so, create middleware
php artisan make:middleware Admin
And add it to the handle function.
if (Auth::check() && Auth::user()->isAdmin()) { return $next($request); } return redirect('/home');
Also create a controller
php artisan make:controller AdminController
Open this controller and replace with this code.
namespace App\Http\Controllers; use Illuminate\Http\Request; class AdminController extends Controller { public function __construct() { $this->middleware('admin'); } public function index() { return view('admin.home'); } }
After go to
app \ http \ Kernel.php
and add the line 'admin' => \App\Http\Middleware\Admin::class, to protected $routeMiddleware 'admin' => \App\Http\Middleware\Admin::class, we get something like this:
protected $routeMiddleware = [ 'auth' => \Illuminate\Auth\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'admin' => \App\Http\Middleware\Admin::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, ];
Open app \ User.php and add a function
public function isAdmin() { return $this->admin;//в таблице users обязательно должно быть колонка `admin` }
You also need to add the admin column in the users table or in the folder
datebase / migrations / create_users_table.php
after $table->string('password'); add $table->integer('admin')->nullable(); and execute the php artisan migrate command
Open routes / web.php
and add this Route::get('/admin', 'AdminController@index');
Create in resoures / views /
admin / home.blade.php // insert any text inside the file.
In the database, give yourself the admin panel and follow the link you-app.dev/admin will show your text in admin / home.blade.php or redirect back to you-app.dev/home. This is the case if you need a panel for the administrator.
if you just want to restrict the rights, create an admin column in the users table and
app / User.php
Insert
public function isAdmin() { return $this->admin;//в таблице users обязательно должно быть колонка `admin` }
Here you have a restriction, and you can check the rights like @if (Auth::user()->isAdmin())
@if(Auth::user()->id == 1)- for this, the model must have access to the table field, see the$fillableproperty. But still I advise you to figure it out and do it right - Vlad