Reputation: 3205
The Laravel documentation says
Note: Renaming columns in a table with a enum column is not currently supported.
What would be a best practice alternative to using an ENUM. For example, I have a users
table with a type
ENUM column. I might need to modify the table in the future, and being unable to because of the existence of an ENUM column is too restrictive.
Upvotes: 0
Views: 697
Reputation: 5876
What i usually do is: Make a types
table.
-----------------------
| id | type |
-----------------------
| 1 | admin |
-----------------------
| 2 | moderator |
-----------------------
In your users
table make a field type_id
. And create your relation in laravel:
class User extends Model
{
public function type()
{
return $this->hasOne('Type');
}
}
Now you can do:
$users = Users::where('type', '=', 1)->get();
Or:
$users = User::with(['type' => function ($query) {
$query->where('type', '=', 'admin');
}])->get();
And you can also inverse the relationship so you can query by type
and load all the users
like:
$all = Type::with('users')->where('type', '=', 'admin')->get();
Upvotes: 3