Skip to content

Commit 54bc1f4

Browse files
committed
Usage documented in readme
1 parent abe9a80 commit 54bc1f4

File tree

1 file changed

+69
-4
lines changed

1 file changed

+69
-4
lines changed

README.md

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# Polymorphic EAV-model integration
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/davidwesdijk/laravel-polymorphic-eav.svg?style=flat-square)](https://packagist.org/packages/davidwesdijk/laravel-polymorphic-eav)
4-
[![Build Status](https://img.shields.io/travis/davidwesdijk/laravel-polymorphic-eav/master.svg?style=flat-square)](https://travis-ci.org/davidwesdijk/laravel-polymorphic-eav)
5-
[![Quality Score](https://img.shields.io/scrutinizer/g/davidwesdijk/laravel-polymorphic-eav.svg?style=flat-square)](https://scrutinizer-ci.com/g/davidwesdijk/laravel-polymorphic-eav)
6-
[![Total Downloads](https://img.shields.io/packagist/dt/davidwesdijk/laravel-polymorphic-eav.svg?style=flat-square)](https://packagist.org/packages/polymorphic-eav/polymorphic-eav)
4+
[![Total Downloads](https://img.shields.io/packagist/dt/davidwesdijk/laravel-polymorphic-eav.svg?style=flat-square)](https://packagist.org/packages/davidwesdijk/laravel-polymorphic-eav)
75

86
Integrate the entity-attribute-value model with polymorphic Eloquent-relations in your Laravel application.
97

@@ -17,8 +15,75 @@ composer require davidwesdijk/laravel-polymorphic-eav
1715

1816
## Usage
1917

18+
### Preparing your models
19+
20+
In order to implement the Entity-Attribute-Value-model to your Eloquent models, the only required action is to add the
21+
`HasEntityAttributeValues` Trait. That's all action required to use the full features this package brings.
22+
23+
``` php
24+
use DavidWesdijk\LaravelPolymorphicEav\Traits\HasEntityAttributeValues;
25+
26+
class Product extends Model
27+
{
28+
use HasEntityAttributeValues;
29+
30+
// Implementation of your Eloquent model
31+
}
32+
```
33+
34+
### Assigning attributes
35+
36+
Attributes are, when the Trait is added, accessable and assignable by the `props` accessor on your Eloquent Model.
37+
Just set its value!
38+
39+
``` php
40+
class ProductController extends Controller
41+
{
42+
public function update(AnyValidatedRequest $request, Product $product)
43+
{
44+
$product->props->details->color = $request->color; // E.g. 'red'
45+
46+
// That's it!
47+
}
48+
}
49+
```
50+
51+
### Accessing attributes
52+
53+
The assigned attributes are simply accessible by defining the group and the attribute in the `props` accessor like
54+
the following example. The concept is that all required properties must be accessible, without throwing exceptions, to
55+
keep you code clean, readable and maintainable.
56+
57+
``` php
58+
// This attribute has been set before
59+
$product->props->details->color; // 'red'
60+
61+
// This attribute has never been set before
62+
$product->props->details->sku; // null
63+
64+
// Even if the group has never been defined
65+
$product->props->foo->bar; // null
66+
```
67+
68+
### Other functionality
69+
70+
If you'd like e.g. remove a property you can either set it's value to null, or you can simply unset the value. The
71+
database entry will be removed.
72+
73+
``` php
74+
unset($product->props->details->color);
75+
76+
$product->props->details->color; // null
77+
```
78+
79+
### Serializing
80+
81+
You might run into a situation where you have to have all assigned properties available in an array or json. This
82+
package respects the `Arrayable` and `Jsonable` interfaces.
83+
2084
``` php
21-
// Usage description here
85+
$product->props->details->toArray(); ['color' => 'red']
86+
$product->props->details->toJson(); '{color: "red"}'
2287
```
2388

2489
### Testing

0 commit comments

Comments
 (0)