Introduction
I recently contributed a new feature to the Laravel framework that allows you to cast model properties to an instance of Illuminate\Support\Uri
. This feature was added in PR #55909 and released in Laravel 12.17.
If you're not familiar with casts, you might be interested in my existing articles which cover them:
- A Guide to Custom Model Casts in Laravel
- Automatically Hash Laravel Model Values Using the "Hashed" Cast
In this Quickfire article, I'm going to quickly cover how to use the new Illuminate\Database\Eloquent\Casts\AsUri
cast in your Laravel models.
Casting Laravel Model Properties to "Illuminate\Support\Uri"
To provide some context about this feature, let's look at an example. Imagine we're building a URL shortener application, and we want to store the destination URL that the user should be redirected to when they visit the shortened URL.
Typically, we might want to treat this URL as a string in our application. However, Laravel now has a handy Illuminate\Support\Uri
which allows us to work with URIs in a more structured way. It provides methods for manipulating and validating URIs, making it easier to work with URLs in our application.
So instead of passing the destination URL around our application as a string, we can cast it to an instance of Illuminate\Support\Uri
instead. To do this, we can define the cast in our model like so:
1declare(strict_types=1); 2 3namespace App\Models; 4 5use Illuminate\Database\Eloquent\Casts\AsUri; 6use Illuminate\Database\Eloquent\Model; 7 8final class ShortUrl extends Model 9{10 protected function casts(): array11 {12 return [13 // ...14 'destination_url' => AsUri::class,15 ];16 }17}
As we can see in the code example above, we've specified that the destination_url
field of our App\Models\ShortUrl
model should use the Illuminate\Database\Eloquent\Casts\AsUri
cast.
This means we can now set the destination_url
field to an instance of Illuminate\Support\Uri
, and it will automatically be converted to a string when saved to the database:
1use Illuminate\Support\Uri; 2 3$shortUrl->destination_url = new Uri( 4 'https://www.example.com:1234/hello?param=value' 5); 6 7$shortUrl->save(); 8 9// The field in the database will now be the following string:10// https://www.example.com:1234/hello?param=value
And vice versa, we can read the destination_url
field like so:
1use Illuminate\Support\Uri; 2 3// Assume the value in the database is the following string: 4// https://www.example.com:1234/hello?param=value 5 6$url = ShortUrl::first()->destination_url; 7 8// $url will now be an instance of Illuminate\Support\Uri 9 10// Illuminate\Support\Uri {11// #uri: League\Uri\Uri {12// -scheme: "https"13// -user: null14// -pass: null15// -userInfo: null16// -host: "www.example.com"17// -port: 123418// -authority: "www.example.com:1234"19// -path: "/hello"20// -query: "param=value"21// -fragment: null22// -uri: "https://www.example.com:1234/hello?param=value"23// }24// }
Conclusion
In this Quickfire article, we've taken a quick look at how to use the new Illuminate\Database\Eloquent\Casts\AsUri
class to cast Laravel model properties to an instance of Illuminate\Support\Uri
. This allows us to work with URIs in a more structured way, providing methods for manipulating and validating URIs.
If you enjoyed reading this post, you might be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.
Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! 🚀