Skip to main content

Laravel Helpers and Blade Components

Premium
3 min read

Comments & Discussion

PC
Paulo Cavalcanti ✓ Link copied!

Hello there.

I have a function in a Service file called getBlendColor, something like this:

function getBlendColor($index): string
{
switch ($index) {
case '4':
$blendColor = 'bg-pink-200';
break;
case '9':
$blendColor = 'bg-blue-200';
break;
case 'total':
$blendColor = 'bg-yellow-200';
break;
default:
$blendColor = '';
break;
}
 
return $blendColor;
}

And i'm calling it inside a blade file like this:

5° Ano

But it is not working. So I put the function on the top of the blade file, and call that function, and it works. I would prefer to have that function separated, but ok. But then i change the switch to match function, to its more elegant, but de colors didn't work properly with the match function, just one color worked.

$blendColor = match ($index) {
'4' => 'bg-pink-200',
'9' => 'bg-blue-200',
'total' => 'bg-yellow-200',
default => '',
};

So, 2 questions:

  1. Can't we use tailwind outside blade, like in a service file and the call then in the blade file?
  2. Why the tailwind did not work with the match funciton properly?
DL
David Lun ✓ Link copied!

Simply add a path to your php file in tailwind.config.js file, so Tailwind knows which files to look for class names. Example:

content: [
'./app/Services/**/*.php',
],

Make sure you're always using complete class names and not constructing them dynamically: Dynamic class names.

PC
Paulo Cavalcanti ✓ Link copied!

Thanks for the reply. I tried this, but it doesn't work for some how. I tried both below and run npm run dev and npm run build:

'./app/Services/**/*.php',
'./app/Services/*.php',

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.