4

I'm having an issue setting an enum column type in MySQL. The column value will be a namespaced classname, such as Accounts\Private.

The whitelisted values should be Accounts\Private and Accounts\Merchant.

Enum seems to be removing the \ from the whitelisted values. Using a double blackslash \\ doesn't work either.

Oddly, adding three blackslashes \\\ causes two backslashes to show.

What am I missing? I'm using Laravel and establishing a polymorphic relationship, which requires the classnames to be namespaced.

Matthew Ruddy
  • 905
  • 4
  • 17
  • 31
  • 1
    http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/ – fancyPants Nov 11 '13 at 15:32
  • 1
    Laravel requires the `account_id` (integer) and `account_type` (string) keys to determine what kind of model the child model belongs to. I was hoping to use `enum` to whitelist `account_type`s. I can just use a standard string otherwise. The mentioned link isn't really appropriate to this use case as we're bound by Laravel conventions. – Matthew Ruddy Nov 11 '13 at 15:36
  • @MatthewRuddy did you ever figure out a way to use MySQL's `enum` with Laravel's polymorphic relationships? I was actually just researching this exact problem and would like to avoid using `varchar` if it is possible. – Cato Minor Jul 20 '15 at 11:57
  • @MatthewRuddy Actually, now that I look around, it looks like the best idea may be not to encounter this problem at all by setting a `$morphClass` in the models and, thus, avoiding namespaces with backslashes in the DB at all. See http://stackoverflow.com/questions/19881963/polymorphic-eloquent-relationships-with-namespaces – Cato Minor Jul 20 '15 at 12:21

1 Answers1

0

I had the same trouble with MySQL enums.

Laravel 5.3 adds a morphMap feature to allow you to set custom strings (without slashes) to use in place of the namespaced classes. Prior to 5.3 there was a morphClass property that could be set on a model.

https://laravel.com/docs/5.3/eloquent-relationships#polymorphic-relations

Zack Huston
  • 426
  • 5
  • 6