The essential news about content management systems and mobile technology. Powered by Perfect Publisher and XT Search for Algolia.
The News Site publishes posts to the following channels: Facebook, Instagram, Twitter, Telegram, Web Push, Tumblr, and Blogger.
Laravel community member Aaron Francis shared an awesome video demonstrating how to Make self-healing URLs with Laravel. Without surprise, multiple packages in the Laravel ecosystem have already surfaced:
These packages have a similar goal: you can change your model's route slug at will or only partially match a URL and never have to worry about SEO indexing when the slug changes. Both packages work by adding a trait to a model (this example is taken from Luke's package), which handles identifying the model via route model binding:
use Lukeraymonddowning\SelfHealingUrls\Concerns\HasSelfHealingUrls;
class Post extends Model
{
use HasSelfHealingUrls;
// Optional custom model property (default is `slug`)
protected $slug = 'title';
}
Note: both packages have different implementations, so be sure to follow the respective README files to get started.
Luke's package also has the ability to customize the way a slug
is joined to a model identifier; you can create your own custom
IdentifierHandler
implementation. At the time of
writing, the package ships with a
HyphenIdentifierHandler
that looks like this:
namespace Lukeraymonddowning\SelfHealingUrls\IdentifierHandlers;
use Illuminate\Support\Str;
use Lukeraymonddowning\SelfHealingUrls\Contracts\IdentifierHandler;
class HyphenIdentifierHandler implements IdentifierHandler
{
public function joinToSlug(string $slug, string|int $identifier): string
{
return "{$slug}-{$identifier}";
}
public function separateFromSlug(string $value): string
{
return Str::afterLast($value, '-');
}
}
You can implement this interface to come up with your customized
handler if you want to use a different identifier than a slug and a
unique identifier separated by a hyphen (-
). I'd
encourage you to watch Aaron's video to see what's going on under
the hood in Laravel and try both implementations to see what you
like.
The post Self-healing URLs in Laravel appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.
Read more https://laravel-news.com/self-healing-urls-in-laravel
Hello everyone, Caen here! I'm excited to share with you a
recent addition I made to the Laravel framework (PR #48845) that I believe will simplify your number
formatting needs. I've introduced a new Number
utility
class that provides several new helpers to format numbers, and it
should be available in the next Laravel release this week.
As another bonus: the helpers are locale-aware, so you can format numbers according to the current locale, either globally, or on a per-method basis!
In many applications, there's often a need to format numbers according to different requirements, such as displaying them as currency, percentages, or human-readable file sizes. Laravel didn't have a dedicated utility for this, so with the help of the community, I decided to create one. I've been working on this utility class for a a little while now, and am really excited that it was merged into the framework. Let's take a look at what it offers.
Number
ClassAll methods are part of the
Illuminate\Support\Number
class:
use Illuminate\Support\Number;
Using the format
method, we can format a number
according to the current locale.
Number::format(25) // 25
Number::format(100000) // 100,000
Number::format(123456789) // 123,456,789
We can also specify a custom locale to format the number according to that locale's rules.
Number::format(123456789, locale: 'en') // 123,456,789
Number::format(123456789, locale: 'de') // 123.456.789
Number::format(123456789, locale: 'sv') // 123 456 789
The formatPercentage
method formats a number as a
percentage according to the current locale.
Number::percentage(25) // 25%
Number::percentage((1/3) * 100, precision: 2) // 33.33%
Here's another fun method used to format various currencies with locale support. Perfect for your webshops!
Number::toCurrency(10) // $10.00
Number::toCurrency(25, currency: 'EUR') // €25.00
Number::toCurrency(5.49, currency: 'EUR', locale: 'de') // 5.49 €
Here is the toFileSize
method which actually is the
whole reason behind this utility class. I first submitted a PR to
add a File::bytesToHuman()
helper (PR #48827), which Taylor then suggested we add as part of a new
Number
class.
Number::toFileSize(1024); // 1 KB
Number::toFileSize(1600, precision: 2); // 1.56 KB
Number::toFileSize(1024 * 1024 * 1024 * 5); // 5 GB
Next up is also a quite fun one for when you want something that's more readable than precise. It converts numbers to a human-readable string.
Number::forHumans(1000) // 1 thousand
Number::forHumans(12345) // 12 thousand
Number::forHumans(12345, precision: 3) // 12.345 thousand
We can set the locale globally using the setLocale
method, for example in a service provider:
Number::setLocale('sv');
You can also use the withLocale
method which
executes the given callback using the specified locale and then
restores the original locale:
Number::withLocale('sv', function () {
return Number::format(123456789);
});
I hope this new addition makes your life a bit easier when dealing with number formatting in Laravel. Please free to check out the next Laravel release and incorporate this utility class into your projects. Happy coding!
Update 11/21/2023: The Numbers helper is now documented in the official Laravel helpers documentation.
The post Introducing the Laravel Number Helpers appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.
Read more https://laravel-news.com/laravel-number-utility-class
The Laravel team released v10.35 with a Blade @use
directive, a number abbreviation helper, the ability to generate a
secret with artisan down
, and more. Here is a bit more
info about the new features introduced this week:
@use()
directiveSimon Hamp contributed a @use()
directive
to import a PHP class into a Blade template without using raw PHP
tags:
{{-- Before --}}
@php
use \App\Enums\WidgetStatusEnum as Status;
@endphp
{{-- After --}}
@use('App\Enums\WidgetStatusEnum', 'Status')
@use('App\Models\Bar')
{{ Status::Foo }}
{{ Bar::first() }}
Number::abbreviate()
method@jcsoriano contributed a
Number::abbreviate()
class to the newly added Number Class, which provides a human-readable
abbreviated number:
Number::abbreviate(1_000_000); // "1M"
Number::abbreviate(100_001); // "100K"
Number::abbreviate(100_100); // "100K"
Number::abbreviate(99_999); // "100K"
Number::abbreviate(99_499); // "99K"
--with-secret
option to the artisan
down
commandJacob Daniel Prunkl contributed a
--with-secret
option to the artisan down
command that will generate a secret phrase that can be used to
bypass maintenance mode so the user doesn't have to define one
themselves:
Conditionable
trait to the
AssertableJson
classKhalil Laleh contributed adding the
Conditionable
trait to the AssertableJson
class, to make it possible to assert based on a given
conditional:
// Before
$response->assertJson(function (AssertableJson $json) use ($condition) {
$json->has('data');
if ($condition) {
$json->has('meta');
}
$json->etc();
});
// After
$response
->assertJson(fn (AssertableJson $json) => $json->has('data'))
->when($condition, fn (AssertableJson $json) => $json->has('meta'))
// ...
;
You can see the complete list of new features and updates below and the diff between 10.34.0 and 10.35.0 on GitHub. The following release notes are directly from the changelog:
Conditionable
trait to
AssertableJson
by @khalilst in
https://github.com/laravel/framework/pull/49172--with-secret
option to Artisan
down
command. by @jj15asmr in
https://github.com/laravel/framework/pull/49171Number::summarize
by
@jcsoriano in https://github.com/laravel/framework/pull/49197The post Laravel 10.35 Released appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.
Read more https://laravel-news.com/laravel-10-35-0
Scout is a Laravel Application Performance Monitoring (APM) tool that pinpoints and prioritizes performance and stability issues, like N+1 database queries, in PHP applications. Scout's tracing logic detects and highlights the exact line of code causing the performance abnormality, and with detailed backtraces, you can fix the issue before customers are impacted. Scout provides further insights by automatically calculating memory increase, tracking object allocations to minimize overhead, and prioritizing query data by potential time saved.
Eloquent performance is typically the main reason for slow Laravel projects. A big part of that is a so-called "N+1 Query Problem". Scout APM includes tracing logic to detect and highlight the exact line causing it.
Automatically report key PHP performance metrics with details on every web endpoint and background job.
Give Scout APM a try free for 14 days and see how it can instantly make your code better by finding errors before your customers do.
The post Solve n+1 queries in PHP with Scout APM appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.
Read more https://laravel-news.com/scoutapm-laravel
It’s almost time for State of the Word 2023! Join us for this live stream event on December 11th at 10am ET.
State of the Word is the annual keynote address delivered by the WordPress project’s co-founder and Automattic CEO, Matt Mullenweg. Every year, the event shares reflections on the project’s progress and the future of open source. Expect all that and more in this year’s edition.
For the first time ever, this event is venturing beyond North America, bringing the WordPress community to a new and vibrant city: Madrid, Spain! The event will be live-streamed to WordPress enthusiasts and newcomers alike via the WordPress YouTube channel.
Join Matt as he provides a retrospective of 2023, demos the latest in WordPress tech, and comments on the future of the WordPress open source project.
What: State of the Word 2023
When: Monday, December 11, 2023 @ 10:00 am ET (15:00 UTC)
How: The live stream is embedded in this post, just above, and will go live at the time of the event. It will also be available through the WordPress YouTube channel. Additionally, there are a number of locally organized watch parties happening around the world if you’d like to watch it in the company of other WordPressers.
Don’t worry, we’ll post the recorded event early next week if you aren’t able to catch it live.
Read more https://wordpress.com/blog/2023/12/05/state-of-the-word-2023/