public function up()
{
// create new staff role
DB::table('roles')->insert(
[
'id' => 2,
'role_name' => 'staff'
]
);
// create default user for staff
DB::table('users')->insert(
[
'username' => 'staff',
'email' => 'staff@test.com',
'password' => Hash::make('password'),
'role_id' => 2,
'created_at' => CarbonCarbon::now()->toDateTimeString(),
'updated_at' => CarbonCarbon::now()->toDateTimeString()
]
);
}
public function down()
{
// delete staff user account
DB::table('users')->where('username', 'staff')->delete();
// delete staff role
DB::table('roles')->where('id', '2')->delete();
}
Create new middleware for roles
php artisan make:middleware RoleMiddleware
Edit Role.php model.
use User;
use Auth;
use Redirect;
public static function userHasRole($role_name)
{
if (Auth::check())
{
$check_role = User::select('roles.role_name')
->join('roles', 'roles.id', '=', 'users.role_id')
->where('users.id', Auth::User()->id)
->where('roles.role_name', $role_name)
->first();
if ($check_role)
{
return true;
}else{
return false;
}
}
return false;
}
Edit the middleware file /app/Http/Middleware/RoleMiddleware.php.
use AppModelsRole;
use Redirect;
public function handle($request, Closure $next, $role)
{
// check if user has role being checked
if (! Role::userHasRole($role))
{
// redirect to access denied page
return back()->with('error', 'Access Denied');
}
return $next($request);
}
Add the new middleware to the /app/Http/Kernel.php.
'role' => AppHttpMiddlewareRoleMiddleware::class,
Apply it in a controller.
public function __construct()
{
$this->middleware('role:admin');
}
I really want to use Windows and Laravel Homestead as a dev environment so that I don’t have to keep pulling out my Macbook Pro when I’m at home. I have a 2015 iMac at home but I do want to utilize the better hardware on my PC. No matter how many times I try,…
Episode 3: Controllers and Move Views Refer to topics covered in Episode 2 Adding assets locally using Node – jQuery and Bootstrap Continue editing the Bootstrap layouts How Controllers are used with Views How to apply active CSS class for controlling style of navbar Adding notification section
I wanted to share how I got Foundation Site 6.3 to work with Laravel 5.4 and webpack. I tried this on a brand new Laravel project. First, edit the package.json file and replace the bootstrap-sass entry with “foundation-sites”: “^6.3.1” Then run npm install. This will download all the dependencies and store them in the node_modules…
I’m not sure when this whole backend vs frontend battle started in my head. But I’m mainly a backend developer. Ever since I started development, I mainly focused on data structure and coding. I rarely paid attention to any of the frontend elements. My main focus were to ensure that my code worked as expected….