Skip to content
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

[8.x] Add unless method on stringable #37326

Merged
merged 2 commits into from
May 10, 2021

Conversation

ph7jack
Copy link
Contributor

@ph7jack ph7jack commented May 9, 2021

This pr aims to increase the code reading by placing the unless function to perform some callback.

Example of use:

$authenticated = false;

Str::of('Hello')
  ->unless($authenticated, function($stringable) {
    return $stringable->append(' Guest!');
    
  })
  ->when($authenticated, function($stringable) {
    return $stringable->append(' ' . auth()->user()->name);
  });

@selcukcukur
Copy link
Contributor

Str::of('Hello')->when($authenticated, function($stringable) {
      return $authenticated 
           ? $stringable->append(' ' . auth()->user()->name)
           : $stringable->append(' Guest!');
});

Why don't you prefer to use it this way?

@ph7jack
Copy link
Contributor Author

ph7jack commented May 9, 2021

@selcukcukur I also need to use only unless, with no other conditions to apply css classes using blade class component

Ex:

Str::of('px-4 py-2 bg-white')
  ->unless($borderless, function($stringable) {
    return $stringable->append(' border border-gray-300!');
  })

That way it is less verbose than using the when method

Str::of('px-4 py-2 bg-white')
  ->when($borderless === false, function($stringable) {
    return $stringable->append(' border border-gray-300!');
  })

@driesvints driesvints changed the title Add unless method on stringable [8.x] Add unless method on stringable May 10, 2021
@taylorotwell taylorotwell merged commit 235db73 into laravel:8.x May 10, 2021
@ph7jack ph7jack deleted the add-stringable-unless-method branch May 10, 2021 15:47
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