Skip to content

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

Merged
taylorotwell merged 1 commit into
laravel:7.xfrom
ManojKiranA:stringablewhen
Jul 7, 2020
Merged

[7.x] Add when method to Stringable class#33455
taylorotwell merged 1 commit into
laravel:7.xfrom
ManojKiranA:stringablewhen

Conversation

@ManojKiranA

@ManojKiranA ManojKiranA commented Jul 7, 2020

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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