0

I am trying to use a case control structure in a Laravel.I can not for the life of me get the required 'blueberry' result even though there are muffins with the 'App\Models\blueberrymuffin' type in the database.

$muffintype=DB::table('muffins')
->select(
DB::raw"(CASE muffin_type WHEN 'App\Models\blueberrymuffin' THEN 'Blueberry' END)" ))->get();

I don't think it is my syntax since when I test this out it works.

$muffintype=DB::table('muffins')
->select(
DB::raw"(CASE milk_type WHEN 'coconut' THEN 'Vegan' END)" ))->get();

I am thinking it has something to do with the ""in the muffin type. The muffin_type is a varchar(255). Does anyone have a clue because I am very perplexed. Thank you in advance

jarlh
  • 42,561
  • 8
  • 45
  • 63
Smithy
  • 13
  • 3

1 Answers1

0

If a string contains forward slashes, those should be escaped:

DB::raw("(CASE muffin_type WHEN 'App\\Models\\blueberrymuffin' THEN 'Blueberry' END)")

Pls also have a look at this thread.

Kevin Bui
  • 2,754
  • 1
  • 14
  • 15