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');
}
Episode 11: Updating My Profile Changes in between episode 10 and 11 Cleaned up code in routes file. Go through code and replaced Role::userHasRole() with User::hasRoles(). Removed __construct() from ProductController. Update database diagram to match current database schema. Create route for /my-profile. Create new route group for auth middleware. Create methods in UserController. myProfile()…
Episode 6: Password Reset and Email Move Login/Logout link to the navbar Add password help link to login page Configure .env to work with mailtrap.io Add value to from so that there is a sent from value. Good idea to use env(‘FROM_EMAIL’) Modify route file with the following // Password reset link request routes…
Episode 2: Views, Layouts, and Elixir Introduction Get episode2 branch from Github. The V in MVC is View. Views are what is being displayed to the user. Views are found in the /resources/views directory. Show how the default is displayed by explaining the routes file and the view file. Show example of .gitignore file….
Episode 7: CRUD Part 1 Changes since the last episode. Switched out laravelcollective code to standard html code. Moved validation messages to part of the form. Updated elixir to version 5. Create a new migration for products table. id int(11) product_name varchar(255) sku varchar(30) price decimal(5,2) description text timestamps php artisan make:migration create_products_table Create…
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….