Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[12.x] Add AsUri model cast #55909

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

Merged
merged 1 commit into from
Jun 2, 2025
Merged

Conversation

ash-jc-allen
Copy link
Contributor

Hey! This PR proposes a new AsUri model cast that will cast values to/from an instance of Illuminate\Support\Uri.

Here's an example of how it could be used in a model for a URL shortener web app:

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\AsUri;
use Illuminate\Database\Eloquent\Model;

class ShortUrl extends Model
{
    protected function casts(): array
    {
        return [
            // ...
            'destination_url' => AsUri::class,
        ];
    }
}

Then we can update the value like so to save a URI:

use Illuminate\Support\Uri;

$shortUrl->destination_url = new Uri('https://www.example.com:1234/hello?param=value');
$shortUrl->save();

// The field in the database will now be the following string:
// https://www.example.com:1234/hello?param=value

And vice versa, we can read the value like so:

use Illuminate\Support\Uri;

// Assume the value in the database is the following string:
// https://www.example.com:1234/hello?param=value

$url = ShortUrl::first()->destination_url;

// $url will now be an instance of Illuminate\Support\Uri

// Illuminate\Support\Uri {
//   #uri: League\Uri\Uri {
//     -scheme: "https"
//     -user: null
//     -pass: null
//     -userInfo: null
//     -host: "www.example.com"
//     -port: 1234
//     -authority: "www.example.com:1234"
//     -path: "/hello"
//     -query: "param=value"
//     -fragment: null
//     -uri: "https://www.example.com:1234/hello?param=value"
//   }
// }

This is something I'd probably use in quite a few of my own applications, so I'd like to think it'd be useful for other developers too. I sometimes do something similar and add my own casts, but thought it could be handy if it's baked into the framework 😄

If this is something you might consider merging, please give me a shout if there's anything you might want me to change. I've done some manual testing and it appears to work as expected, but I do apologise if I've missed anything!

@markhuot
Copy link
Contributor

markhuot commented Jun 2, 2025

I love this. But AsUri seems super useful for a lot of casts, not just URIs since it’s really just a null check. I wonder if rebranding the AsUri to a null check would be more powerful?

‘ destination_url’ => CastWhen::filled(\Illuminate\Support\Uri::class),

@taylorotwell taylorotwell merged commit 98c0b02 into laravel:12.x Jun 2, 2025
61 checks passed
@ash-jc-allen ash-jc-allen deleted the uri-cast branch June 2, 2025 14:26
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.

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.