45
loading...
This website collects cookies to deliver better user experience
constants.php
file carrying a long list of all constants used across the application. constants.php
file looks like this:const ADMIN_ROLE = 1;
const EDITOR_ROLE = 2;
const PUBLISHER_ROLE = 3;
roles.php
:<?php
return [
'admin' => 1,
'editor' => 2,
'publisher' => 3
];
config('roles.admin'); // Using helper functions
Config::get('roles.admin'); // Using facades
constants.php
file is that it uses the framework's original mechanics to store and access values. Laravel configurations are familiar to most developers, and so people new to your code won't have to learn something new to use them.User
model class:class User extends Model {
const ADMIN_ROLE = 1;
const EDITOR_ROLE = 2;
const PUBLISHER_ROLE = 3;
public function isAdmin(){
return $this->role === static::ADMIN_ROLE;
}
}
Role
class that contains all values/logic related to roles:class Role {
const ADMIN = 1;
const EDITOR = 2;
const PUBLISHER = 3;
}
class User extends Model {
public function isAdmin(){
return $this->role === Role::ADMIN;
}
}
users
table, you may use the enum
function to create your column:public function up()
{
Schema::create('users', function (Blueprint $table)
{
$table->enum('role', ['admin', 'editor', 'publisher']);
// Other columns...
});
}
class User extends Model {
public function isAdmin(){
return $this->role === 'admin';
}
}
constants.php
file? Where else do you place your constants?