Laravel query builder raw case

Viewed 44

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

1 Answers

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.

Related