[8.x] Add Str::initials() helper to get initials of a given value #40381
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In Jetstream laravel/jetstream#940, @geisi has introduced anonymized default profile photo url calls by only extracting the initials of a user's name. It would have been nice if he could have used an existing Laravel String helper function.
I am introducing
Str::initials()
for this purpose and hope it helps you in other use cases.For those who already want to use this before it's accepted/merged, just extend
Illuminate\Support\Str
with this macro in yourAppServiceProvider
'sboot()
method:and then just use
Str::initials('Firstname Lastname')
or as an Eloquent accessor in yourUser
model:so you can simply use something like
{{ Auth::user()->initials }}
.