All Projects → jessarcher → Laravel Castable Data Transfer Object

jessarcher / Laravel Castable Data Transfer Object

Licence: mit
Automatically cast JSON columns to rich PHP objects in Laravel using Spatie's data-transfer-object class

Projects that are alternatives of or similar to Laravel Castable Data Transfer Object

Laravel Auto Translate
Automatically translate your language files using a translator service
Stars: ✭ 153 (-19.9%)
Mutual labels:  laravel, laravel-package
Laravel Cross Eloquent Search
Laravel package to search through multiple Eloquent models. Supports sorting, pagination, scoped queries, eager load relationships and searching through single or multiple columns.
Stars: ✭ 189 (-1.05%)
Mutual labels:  laravel, laravel-package
Laravel Early Access
This package makes it easy to add early access mode to your existing application.
Stars: ✭ 160 (-16.23%)
Mutual labels:  laravel, laravel-package
Servermonitor
💓 Laravel package to periodically monitor the health of your server and application.
Stars: ✭ 148 (-22.51%)
Mutual labels:  laravel, laravel-package
Laravel Auth Checker
Laravel Auth Checker allows you to log users authentication, devices authenticated from and lock intrusions.
Stars: ✭ 177 (-7.33%)
Mutual labels:  laravel, laravel-package
Laravelresources
Speed Up package development for Laravel Apps with API's
Stars: ✭ 152 (-20.42%)
Mutual labels:  laravel, laravel-package
Telegram Bot Sdk
🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
Stars: ✭ 2,212 (+1058.12%)
Mutual labels:  laravel, laravel-package
Laravel Api Explorer
API explorer for laravel applications
Stars: ✭ 138 (-27.75%)
Mutual labels:  laravel, laravel-package
Laravel Source Encrypter
Laravel and Lumen Source Code Encrypter
Stars: ✭ 175 (-8.38%)
Mutual labels:  laravel, laravel-package
Laravel Selfupdater
This package provides some basic methods to implement a self updating functionality for your Laravel application. Already bundled are some methods to provide a self-update mechanism via Github or some private repository via http.
Stars: ✭ 170 (-10.99%)
Mutual labels:  laravel, laravel-package
Laravel Themer
Multi theme support for Laravel application
Stars: ✭ 142 (-25.65%)
Mutual labels:  laravel, laravel-package
Laravel Localization Helpers
🎌 Artisan commands to generate and update lang files automatically
Stars: ✭ 190 (-0.52%)
Mutual labels:  laravel, laravel-package
Laravel Db Profiler
Database Profiler for Laravel Web and Console Applications.
Stars: ✭ 141 (-26.18%)
Mutual labels:  laravel, laravel-package
Laravel Adminer
Adminer database manager for Laravel 5+
Stars: ✭ 185 (-3.14%)
Mutual labels:  laravel, laravel-package
Laravel Scout Postgres
PostgreSQL Full Text Search Engine for Laravel Scout
Stars: ✭ 140 (-26.7%)
Mutual labels:  laravel, laravel-package
Laravel Security Checker
Added Laravel functionality to Enlightn Security Checker. Adds a command to check for, and optionally emails you, vulnerabilities when they affect you.
Stars: ✭ 163 (-14.66%)
Mutual labels:  laravel, laravel-package
Laravel Deletable
👾 Gracefully restrict deletion of Laravel Eloquent models
Stars: ✭ 137 (-28.27%)
Mutual labels:  laravel, laravel-package
Laravel Easypanel
A beautiful and flexible admin panel creator based on Livewire for Laravel
Stars: ✭ 135 (-29.32%)
Mutual labels:  laravel, laravel-package
Laravel Page Speed
Package to optimize your site automatically which results in a 35%+ optimization
Stars: ✭ 2,097 (+997.91%)
Mutual labels:  laravel, laravel-package
Laravel Identify
📦 📱 Laravel 5 Package to Detect Users Browsers, Devices, Languages and Operating Systems
Stars: ✭ 177 (-7.33%)
Mutual labels:  laravel, laravel-package

Laravel Castable Data Transfer Object

Latest Version on Packagist Quality Score Total Downloads

Laravel is awesome. Spatie's data transfer object package for PHP is awesome. They're already good friends, but now they're taking their relationship to the next level 💕

Have you ever wanted to cast your JSON columns to a value object?

This package gives you an extended version of Spatie's DataTransferObject class, called CastableDataTransferObject.

Under the hood it implements Laravel's Castable interface with a Laravel custom cast that handles serializing between the DataTransferObject (or a compatible array) and your JSON database column.

For an in-depth explanation of what it's actually doing and the motivation behind it, check out the blog post that spawned it.

This package has also been featured on Laravel News!

Installation

You can install the package via composer:

composer require jessarcher/laravel-castable-data-transfer-object

Usage

1. Create your CastableDataTransferObject

Check out the readme for Spatie's data transfer object package to find out more about what their DataTransferObject class can do.

namespace App\Values;

use JessArcher\CastableDataTransferObject\CastableDataTransferObject;

class Address extends CastableDataTransferObject
{
    public string $street;
    public string $suburb;
    public string $state;
}

(Note: I like to put these in App\Values because I'm using them as a value object and not just a plain DTO. Feel free to put it anywhere you like!)

2. Configure your Eloquent attribute to cast to it:

Note that this should be a jsonb or json column in your database schema.

namespace App\Models;

use App\Values\Address;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $casts = [
        'address' => Address::class,
    ];
}

And that's it! You can now pass either an instance of your Address class, or even just an array with a compatible structure. It will automatically be cast between your class and JSON for storage and the data will be validated on the way in and out.

$user = User::create([
    // ...
    'address' => [
        'street' => '1640 Riverside Drive',
        'suburb' => 'Hill Valley',
        'state' => 'California',
    ],
])

$residents = User::where('address->suburb', 'Hill Valley')->get();

But the best part is that you can decorate your class with domain-specific methods to turn it into a powerful value object.

$user->address->toMapUrl();

$user->address->getCoordinates();

$user->address->getPostageCost($sender);

$user->address->calculateDistance($otherUser->address);

echo (string) $user->address;

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].