Download the PHP package vkovic/laravel-custom-casts without Composer

On this page you can find all versions of the php package vkovic/laravel-custom-casts. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-custom-casts

Laravel Custom Casts

Build Downloads Stable License

Make your own cast type for Laravel model attributes

Laravel custom casts works similarly to Eloquent attribute casting, but with custom-defined logic (in a separate class). This means we can use the same casting logic across multiple models — we might write image upload logic and use it everywhere. In addition to casting to custom types, this package allows custom casts to listen and react to underlying model events.

Let's review some Laravel common cast types and examples of their usage:

In addition to boolean, integer, and decimal, out of the box Laravel supports real, float, double, string, object, array, collection, date, datetime, and timestamp casts.

Sometimes it is convenient to handle more complex types with custom logic, and for casts to be able to listen and react to model events. This is where this package come in handy.

Handling events directly from custom casts can be very useful if, for example, we're storing an image using a custom casts and we need to delete it when the model is deleted. Check out the old documentation for this example.

:package: vkovic packages :package:

Please check out my other packages — they are all free, well-written, and some of them are useful :smile:. If you find something interesting, consider giving me a hand with package development, suggesting an idea or some kind of improvement, starring the repo if you like it, or simply check out the code - there's a lot of useful stuff under the hood.

Compatibility

The package is compatible with Laravel versions 5.5, 5.6, 5.7, 5.8 and 6

and Lumen versions 5.5, 5.6, 5.7, 5.8.

Laravel 7+ has native support for Custom Casts that are incompatible with this library.

Minimum supported version of PHP is 7.1. PHP 8 is also supported.

Installation

Install the package via Composer:

Usage

Utilizing a custom cast class

To enable custom casts in a model, use the HasCustomCasts trait and define which attributes will be casted using $casts - per Laravel standards.

Defining a custom cast class

This class will be responsible for our custom casting logic.

The required setAttribute method receives the $value being set on the model field, and should return a raw value to store in the database.

The optional castAttribute 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.

For the sake of this example we'll implement one more method which will attach a random title to a user when their name is retrieved from database.

Testing a custom cast class

Let's create a user and see what happens.

This will create our new user and store their name in the database, with the first letter of each word uppercased.

When we retrieve the user and try to access their name, title will be prepended to it — just like we defined in our custom NameCast class.

Handling model events

Let's say that we want to notify our administrator when a user's name changes.

In addition to the updated method, we can define other methods for standard model events: retrieved, creating, created, updating, saving, saved, deleting, deleted, restoring and restored.

Other functionality

As you can see from the above code, we can easily access the casted attribute name as well as an instance of the underlying model.

We can also retrieve all casted attributes and their corresponding classes directly from the model.

Using aliased casts

You may find it easier to use aliases for custom casts, e.g.:

To make the magic happen, first add the package's service provider to the providers array:

Once the provider is added, publish the config file which will be used to associate aliases with their corresponding custom cast classes:

This command should create a config file located at config/custom_casts.php. Open it up and check out the comments for examples of config options.

Use it without Laravel

This package can also be used without full Laravel installation, with something like jenssegers/model or if your project is using illuminate/database library.

More examples

You can find more examples in the old documentation.

Contributing

If you plan to modify this Laravel package you should run the tests that come with it. The easiest way to accomplish this is with Docker, docker-compose, and phpunit.

First, we need to initialize Docker container (see docker-composer.yaml for details).

After that, we can run tests and watch the output:


All versions of laravel-custom-casts with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1|^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package vkovic/laravel-custom-casts contains the following files

Loading the files please wait ....