Skip to content

[12.x] Add @blank and @filled Blade directives #56192

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

Closed
wants to merge 1 commit into from

Conversation

kichetof
Copy link
Contributor

@kichetof kichetof commented Jul 1, 2025

This PR add 2 new Blade directives: @blank and @filled who works as a shortcuts of Laravel blank and filled functions.

These directives are particularly useful when you have Countable or Stringable object and want to display/hide something as you can't use @isset or @empty directives to check statement.

Before this PR

You need to call isEmpty/ isNotEmpty

@if($collection->isEmpty())
    Display something
@endif

// or 

@if($collection->isNotEmpty())
    Display something
@endif

With this PR

Simply call

@blank($collection)
    Display something 
@endblank

// or 

@filled($collection)
    Display something
@endfilled

@rodrigopedra
Copy link
Contributor

I literarily add these two directives to every project.

<?php

namespace App\Providers;

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Compilers\BladeCompiler;

class BladeServiceProvider extends ServiceProvider implements DeferrableProvider
{
    public function register(): void
    {
        $this->callAfterResolving(BladeCompiler::class, static function (BladeCompiler $compiler) {
            $compiler->if('filled', static fn($value) => \filled($value));
            $compiler->if('blank', static fn($value) => \blank($value));
        });
    }

    public function provides(): array
    {
        return [
            BladeCompiler::class,
        ];
    }
}

@devajmeireles
Copy link
Contributor

We've tried adding it to other PR. In the past, I was okay with it, but now I think it's really overkill. A simple if/else can solve this.

@if (blank(...))

@if (filled(...))

@browner12
Copy link
Contributor

I agree, this feels like overkill just to save a couple characters.

@kichetof
Copy link
Contributor Author

kichetof commented Jul 2, 2025

Totally agree with you. These are shortcuts like @isset or @empty. As simple as it is useful.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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.

5 participants