vkovic / laravel-custom-casts Goto Github PK
View Code? Open in Web Editor NEWMake your own custom cast type for Laravel model attributes
License: MIT License
Make your own custom cast type for Laravel model attributes
License: MIT License
The optional getAttribute method receives the raw $value from the database, and should return a mutated value. If this method is omitted, the raw database value will be returned.
Is it really getAttribute
or rather castAttribute
? The example is kinda vague about this.
Laravel 6 is released. It would be nice to have this package updated to support Laravel 6.
In native laravel cast, you can add params for casts. For example date.
Adding this feature gives more control over casting process.
class User extends Model {
protected $casts = [
'avatar' => 'file:disk',
];
}
To give people to distribute their custom casts, we have to add the ability to dynamically register new casts. In laravel such functionality is done using Manager::class
and extend
method.
class PackageServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->make(CustomCastManager::class)->extend('file', FileCast::class);
}
}
When the attribute is not present in a create/update call, null is inserted. This causes issues with non nullable columns with default values.
According to your composer you are strictly targeting Laravel framework, however inside you are using only it's components like eloquent and maybe it's dependencies.
All the functionality can be easily reused inside Lumen framework (micro-framework of Laravel) cause it also has support for Eloquent usage. But unfortunately your composer requires from me specially Laravel.
Pls change your dependencies to concrete illuminate packages versions so this package can be installed on both Laravel and Lumen
Tests failing because of deprecation of str_*
functions.
We should use Illuminate\Support\Str
instead.
Hi
It seems your package is incompatible with Laravel v5.8.
ฮป composer require vkovic/laravel-custom-casts
Using version ^1.0 for vkovic/laravel-custom-casts
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Conclusion: remove laravel/framework v5.8.14
- Conclusion: don't install laravel/framework v5.8.14
- vkovic/laravel-custom-casts v1.0.0 requires laravel/framework 5.5.*|5.6.*|5.7.* -> satisfiable by laravel/framework[5.5.x-dev, 5.6.x-dev, 5.7.x-dev].
- vkovic/laravel-custom-casts v1.0.1 requires laravel/framework 5.5.*|5.6.*|5.7.* -> satisfiable by laravel/framework[5.5.x-dev, 5.6.x-dev, 5.7.x-dev].
- Can only install one of: laravel/framework[5.5.x-dev, v5.8.14].
- Can only install one of: laravel/framework[5.6.x-dev, v5.8.14].
- Can only install one of: laravel/framework[5.7.x-dev, v5.8.14].
- Installation request for laravel/framework (locked at v5.8.14, required as 5.8.*) -> satisfiable by laravel/framework[v5.8.14].
- Installation request for vkovic/laravel-custom-casts ^1.0 -> satisfiable by vkovic/laravel-custom-casts[v1.0.0, v1.0.1].
Installation failed, reverting ./composer.json to its original content.
Idea is to add the ability to use chained casts. Example
class User extends Model {
protected $casts = [
'meta' => ['json', CastToMetadataClass::class],
];
}
with this you can build a lot of reusable parts and combine them
Describe the bug
Running
$email = Network\Email::firstOrCreate( $emailParams );
also tried
$email = new Network\Email;
$email->firstOrCreate( $emailParams );
Those don't run the data conversion.
Although running...
$email = Network\Email::create( $emailParams );
Does run the conversion just fine.
To Reproduce
My cast class is
class PostgresArray extends CustomCastBase
{
public function setAttribute($value) {
if (is_string($value)) return $value;
if (is_array($value)) return "{" . implode(',', $value) . "}";
}
public function castAttribute($value) {
if (is_string($value)) return $value;
if (is_array($value)) return "{" . implode(',', $value) . "}";
}
}
I'm on the latest current version of your library, using Laravel 6.
I had added dd
commands in my cast to make sure it wasn't executing.
Am I missing something from my cast or is this a limitation with the library?
Thanks!
Is your feature request related to a problem? Please describe.
Laravel 9 supports AttributeCasting for ValueObject for example class money
I wanna this library to support ValueObject casting.
Describe the solution you'd like
We probably should add a way to allow to return of multiple value for setAttribute via hashmap or via some custom DTO object SetAttributes
public function setAttribute($value)
{
return ucwords($value);
}
We should return
Describe alternatives you've considered
I would like to know if this is the same as laravel custom cast attributes in laravel 7...
Is this the same repo merge in the laravel framework or different?
What are the difference with this repo to the one being used by the framework
thanks
I think it would be awesome to be able to specify the cast name in human readable format:
protected $casts = [
'amount' => MoneyCast::class
];
becomes
protected $casts = [
'amount' => 'money'
];
Perhaps the approach used could be similar to how Laravel infers the policy class names using the guessPolicyNamesUsing
method.
How difficult would this be to add? Not super familiar with this package yet so wasn't sure how much effort is involved (although happy to help out as well!)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.