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

[7.x] Add when method to Stringable class #33455

Merged
merged 1 commit into from
Jul 7, 2020
Merged

[7.x] Add when method to Stringable class #33455

merged 1 commit into from
Jul 7, 2020

Conversation

ManojKiranA
Copy link
Contributor

@ManojKiranA ManojKiranA commented Jul 7, 2020

Added when Method to Stringable class.

Use case:

For example i have restrict or limit some of the contents for unauthenticated users i can use auth check and based on that i can so that.

$blogContent = 'Laravel Factories Reloaded is a package by Christoph Rumpel that generates class-based model factories that you can use instead of the factory files Laravel provides';

//BEFORE
$stringable = Str::of($blogContent);

if(!Auth::check()){

    $stringable->limit(20,'.....')
                    ->append('To Continue reading ')
                    ->append(new HtmlString('<a href="#">Get a Subscription</a>'));
}

$stringable->__toString();


//AFTER
Str::of($blogContent)
    ->when(!Auth::check(),function($stringable){
        return $stringable->limit(20)
                        ->append('To Continue reading ')
                        ->append(new HtmlString('<a href="#">Get a Subscription</a>'));
    })
    ->__toString();

I have been using when method on query builder and its really cleans up some if else so same in the Stringable too

@ManojKiranA
Copy link
Contributor Author

I am not sure how to fix the StyleCI. I hope before a new release every week StyleCI will be fixed right??.

@ManojKiranA
Copy link
Contributor Author

I will send a pr to docs if it merged

@driesvints driesvints changed the title Add when method to Stringable class [7.x] Add when method to Stringable class Jul 7, 2020
@taylorotwell taylorotwell merged commit 8225119 into laravel:7.x Jul 7, 2020
@ManojKiranA ManojKiranA deleted the stringablewhen branch July 8, 2020 02:06
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.

2 participants