Get a Free Quote
Our Blog

Schemaless Attributes Package : Complete overview

152 total views
Tarun Bansal
June 07, 2018
Schemaless Attributes Package

Laravel is a PHP based web framework used to develop web applications following the MVC architecture. Laravel development is in a huge demand these days in the IT world and the Laravel development services release new features to enhance the development and to make it smooth and fast.

Schemaless attributes package is the new buzzword in the Laravel development. You can add schemaless attributes to Eloquent relational models with the Laravel schemaless attributes package by the Laravel development services. The Schemaless attributes package is just wonderful to use because when it is applied on a model, it allows you to store arbitrary values in the single JSON column. The package requires a database with support for JSON columns and so you will need to use a MySQL 5.7 database or higher.

The method for getting and setting schemaless attributes is the same way you set the attributes:

$yourModel->extra_attributes->name = 'value';
$yourModel->extra_attributes->name; // Returns 'value'
For saving the schemaless attributes, save the model:
// Persists both normal and schemaless attributes
$yourModel->save();

Now, querying the model’s extra attributes:

$yourModel->withExtraAttributes([
'name' => 'value',
'name2' => 'value2
])->get();

Install the package with the composer and go ahead with the package:

composer require spatie/laravel-schemaless-attributes

Next step is to add the following to a model migration that you want the attributes of JSON:

Schema::table('your_models', function (Blueprint $table) {
$table->schemalessAttributes('extra_attributes');
});

You can check the documentation for the additional steps needed to prepare a model for the schemaless data, it is always useful. If your plan is to use schemaless data on the multiple models, the documentation shows you how to create a trait for the extra attributes. The Laravel development services have provided a great help by providing the developers with this package. The package is really helpful to add and access the schemaless data inside a relational database and other methods that help when you are working with the JSON data.

You will need to add a cast, a scope, and an accessor on the model in order to work with the schemaless attributes. This example will show you what you need to add if you have chosen extra_attributes as your column name.

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Spatie\SchemalessAttributes\SchemalessAttributes;

class TestModel extends Model
{
// ...

public $casts = [
'extra_attributes' => 'array',
];

public function getExtraAttributesAttribute(): SchemalessAttributes
{
return SchemalessAttributes::createForModel($this, 'extra_attributes');
}

public function scopeWithExtraAttributes(): Builder
{
return SchemalessAttributes::scopeWithSchemalessAttributes('extra_attributes');
}

// ...
}

You can also reuse this behavior across multiple models by opting the function in your own trait. For example:

namespace App\Models\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Spatie\SchemalessAttributes\SchemalessAttributes;

trait HasSchemalessAttributes
{
public function getExtraAttributesAttribute(): SchemalessAttributes
{
return SchemalessAttributes::createForModel($this, 'extra_attributes');
}

public function scopeWithExtraAttributes(): Builder
{
return SchemalessAttributes::scopeWithSchemalessAttributes('extra_attributes');
}
}

Thus, this was the overview for the Schemaless Attribute Package of the Laravel development services.