Download the PHP package kouks/laravel-casters without Composer

On this page you can find all versions of the php package kouks/laravel-casters. 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-casters

Laravel Casters

Scrutinizer Code Quality Latest Version on Packagist Build Status StyleCI

Contents

Installation

Composer

Open your console and cd into your Laravel project. Run:

Yeah, you are pretty much ready to go right now...

Usage

Creating casters

You can put you casters pretty much anywhere, but I suppose you put them in a app/Casters directory. Now make a new php class, we are gonna be casting a Post model here. Suppose our model has only id, title, body and active database columns, along with timestamps.

Good! Now we can put in our first casting rule.

This caster is going to cast only the title column, leaving its contents unchanged. More about making rules in the follwing section.

Making your rules

Let's move on how to make your own rules, there are four ways to achieve that at this moment.

Simple casting/renaming columns

There was an example of a simple cast rule in the previous section. Let us reviews it again and att something up.

This rule will remain the title column unchanged completely, and renaming the body column to long_text, however leaving the contents unchanged.

Using cast queries

Similar to Laravel's validation rules, you can use queries to cast.

Note the ! before the cast rule. This says that we want to use a query. This simple query renames the active column to is_active and casts its contents to a boolean. All documentation on cast queries can be found in the Cast queries section.

Casting using a closure

As a value of the cast rules, you can speficy a closure, which will determine what data to return.

This particular query is going to cast a new column body and as its contents it is going to use whatever is returned from the closure. In this case - the post body limited to 100 characters. Note that you are given an instance of the model that is being cast in the closure arguments.

Casting using a method

You can even use other methods on the caster class to determine what is going to be cast.

Notice the @ sign - it is to determine that we want to use a caster class method to do the casting. This cast is (similarly to the closure one) create a new column draft, which is just a negation of the active column in this example. You are given the model instance in this case, too.

Actual casting

The actual casting gets really simple, there are two ways to cast you data at this moment.

Casting a single model

Let us have a controller action show, which is going to cast our model, and retrun it as json.

Note that you can use DI for your casters, as with anything in Laravel. Then you call a single cast method and provide you model instance in the arguments. If we were to combine all the example casters, the returned json could look following:

Note that in the example above, the text field is going to be limited to 100 characters.

Casting a collection

You cast collections in the same way that you would cast single models. Only difference is that you are given back array of cast models, instead of a single one.

Casting directly from model instance

This package provides the Koch\Casters\Behavior\Castable trait, which lets you do both casting a single model as follows:

and a collection directly from you query:

Also note that if you specify a full class path to the caster in you model, you can omit the caster variable completely.

Lets you do this fancy stuff:

If you follow the convention of storing your casters in the app/Casters directory as well as the naming conventions, this package will try to find related caster in that location - this lets you leave out even the caster property.

Casting relationships

There, obviously, is a way to cast relationships. Suppose there is a related model Comments, wich has a many-one relationship with our Post model. Also suppose that there is a CommentCaster set up. Look at the following code:

Note that even here you can levarage Laravel's DI. You now create a new column comments, which is then populated by the closure. This closure usis the injected caster to cast the comments relationship. You can do the same with casting via methods.

Also beware of cycling your casts. At the moment, there is no check for that. So if you were to cast your comments relationship on the PostCaster and do the same vice versa, you'd end end with a 500. Feel free to submit a PR correcting this issue.

Cast queries

At this moment, there are only two queries. I am open for suggestions to add more.

Abstracion

There is a Koch\Casters\Contracts\Caster interface, which you could use for example with the repository pattern, where you have a parent Repository class which deals with all the stuff around any model, including casting it. You might want to inject the caster though a PostRepository constructor, in which case, the parent class would require the contract.

FAQ

Nobody has ever asked me anything about this package so I can't determine the frequency of questions.


All versions of laravel-casters with dependencies

PHP Build Version
Package Version
Requires php Version >=5.6.4
illuminate/database Version ^5.3
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 kouks/laravel-casters contains the following files

Loading the files please wait ....