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

Skip to content

[12.x] Improve @use directive to support function and const modifiers #55583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 29, 2025

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.

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.

3 participants