Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@rodolfosrg
Copy link
Contributor

@rodolfosrg rodolfosrg commented Apr 28, 2025

What's New? πŸŽ‰

The @use directive in Laravel just got a major upgrade!
Before, it only supported two parameters: a path and an alias.
Now you can also specify function 🧩 or const πŸ”₯ imports directly within Blade templates!

βœ… Example

@use(function App\Helpers)
@use(const App\Constants\MY_LITTLE_CONST, MY_LITTLE_ALIAS)

βœ… Grouped imports still work beautifully

@use(function App\Helpers\{foo, bar})
@use(const App\Constants\{FOO, BAR})

βœ… Paths still work with or without leading slashes

@use(function \App\Helpers)

Why is this awesome? πŸ§ πŸ’‘

  • More readable πŸ“: No need for messy <?php tags inside Blade views!
  • Fewer lines 🧹: @use keeps your templates clean and tidy compared to multiple PHP use imports.
  • More idiomatic 🎨: Feels natural and consistent with Blade syntax, avoiding context switching between PHP and Blade.
  • Blade power-up ⚑: Leverage PHP constants and functions elegantly without breaking out of your Blade flow.
  • Future-friendly πŸ›‘οΈ: Makes Blade templates more maintainable and expressive!

TL;DR 🏎️

@use isn't just for classes anymore - now it's for functions and constants too!
Write cleaner, sharper Blade views without sacrificing PHP power! πŸ§™β€β™‚οΈβœ¨

@taylorotwell taylorotwell merged commit d74311d into laravel:12.x Apr 29, 2025
58 checks passed
@christophrumpel
Copy link
Contributor

Hey @rodolfosrg, I'm trying to import a constant but can't get it working. What am I missing here?

<?php

namespace App;

class Lyrics
{
    public const HELP_SOMEBODY = 'I need somebody';
    public const HELP_ANYBODY = 'Not just anybody';
    public const HELP_SOMEONE = 'You Know I need someone';
}
@use(const App\Lyrics\HELP_SOMEBODY)

<x-layout-hero>
    <p>{{ HELP_SOMEBODY }}</p>
</x-layout-hero>

Error: Undefined constant "App\Lyrics\HELP_SOMEBODY"

@rodolfosrg
Copy link
Contributor Author

Hey @christophrumpel,

You're importing a class constant which uses the ClassName::CONSTANT syntax. I didn't test these cases, but this should work:

@use(const App\Lyrics::HELP_SOMEBODY)

<x-layout-hero>
    <p>{{ HELP_SOMEBODY }}</p>
</x-layout-hero>

@christophrumpel
Copy link
Contributor

Oh wow, I believe I never used "non-class-constants". These do work. πŸ‘

Class constants I couldn't get to work.

@osbre
Copy link
Contributor

osbre commented Oct 7, 2025

@christophrumpel FYI such behaviour matches PHP, since you cannot import class constants, only the class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants