serafim/accessor

This package is abandoned and no longer maintained. No replacement package was suggested.

The Laravel Eloquent model custom accessors.

dev-master / 1.0.x-dev 2014-12-18 16:38 UTC

README

Laravel 4 Eloquent custom fields accessor: See on packagist.org

Build Status Code Quality

Installation

  • Open composer.json and write
{
    "require": {
        "serafim/accessor": "dev-master@dev"
    }
}
  • composer update

Usage

  • Include Serafim\Accessor\FieldsAccessorTrait inside your Eloquent model
  • Write protected static $fields = ['my_array_field' => 'Set'];. Where 'my_array_field' - field name, Set - type
  • Database field must be type of text

Example

<?php
// Model
class BlogComment extends Eloquent
{
    use Serafim\Accessor\FieldsAccessorTrait;
    protected static $fields = ['user_reads' => 'Set']; // Users who read this comment
}
 
// Usage
$comments = BlogComment::find(1);
$comments->user_reads[] = Auth::user()->id; // Add auth user id
echo $comments->user_reads; // [first_user_id, second_user_id]
$comments->save();

Methods

Set (SetContainer)

Can store unique values. Suitable for storing tags, users vote in polls (small data) and other

->has($item) - has item in set

->clear() - clear data

->isEmpty() - returns true if empty

->notEmpty($item) - returns false if empty

->add($item) - add new item

->remove($item) - remove item

->values() - return values (alias toArray)

Structure
[value, value, value]

Map (MapContainer) and Array (ArrayContainer)

Allows you to store a pair of key - value. Suitable for saving filters (for example, discounts on goods in online store), labels on the map and other similar data.
  • Array can contains only positive integer values

->push($value) - add new value at and

->unshift($value) - add new value at start

->pop() - return last value and remove him

->shift() - return first value and remove him

->merge($container) - merge with another container

->isEmpty() - returns true if empty

->notEmpty() - returns false if empty

->exists($key) - return true if value with target key exists in container

->has($value) - return true if value exists in container

->get($key) - return value by key

->set($key, $value) - add new value with target key (*Array can contains only positive integer values)

->keys() - return all keys in container

->values() - return all values in container

->clear() - remove all data from container

Structure
[0 => value, 1 => value, 2 => value] // Array and Map
[key => value, key => value, key => value] // only Map