2017 © Pedro Peláez
 

library embed-relation

a relation for laravel

image

tusimo/embed-relation

a relation for laravel

  • Wednesday, May 23, 2018
  • by tusimo
  • Repository
  • 1 Watchers
  • 2 Stars
  • 1,358 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 54 % Grown

The README.md

embed-relation, (*1)

Latest Stable Version Total Downloads

add a new relation is missing from Laravel's ORM. embedsMany extends Eloquent ORM ., (*2)

Installation

Either PHP 5.6+ is required., (*3)

To get the latest version of embedsMany, simply require the project using Composer:, (*4)

$ composer require tusimo/embed-relation

Instead, you may of course manually update your require block and run composer update if you so choose:, (*5)

{
    "require": {
        "tusimo/embed-relation": "^0.1"
    }
}

Usage

Within your eloquent model class add following line And when we have a json string column as data. We support virtual column and can use cast. And support some new cast, 'integer_array', 'string_array', 'float_array', 'bool_array', (*6)

class User extends Model {
    use \Tusimo\Eloquent\Traits\EmbedsRelation;
    use \Tusimo\Eloquent\Traits\CastAttributes;

    protected $virtualColumnMaps = [
        'data' => [
            'address' => 'home_address',//you can rename the column
            'follower_ids'
        ],
        //'more_json_data' => [],
    ];

    protected $casts = [
        'book_ids' => 'integer_array',
        'home_address' => 'string',
        'follower_ids' => 'integer_array',
    ];
    ...
}

Example:

Consider User has several favorite books and the book_ids just store in the user table as book_ids column. We want this column can to load use relations. So we can do it like this. We have user table just like this., (*7)

id user_name book_ids data
1 tusimo 1,2,3 {"address":"NY","follower_ids":"1,3"}
2 john 2,4,7 {"address":"WD","follower_ids":"3"}
3 aly 5 {"address":"LA","follower_ids":"1,2"}

and book table like this,, (*8)

id book
1 css
2 php
3 javascript
4 database
5 sql
6 python
7 html
class User extends Model {
    use \Tusimo\Eloquent\Traits\EmbedsRelation;

    public function books () {
        return $this->embedsMany(Book::class);
    }
}

If we want to get the books so we can use$user->books. For now I just finished get relation data. Next I will do the save thing And the reverse relation., (*9)

Now we can access data like this ., (*10)

    $user->home_address = 'HA';
    $user->follower_ids = [1,2,3,4];
    $user->save();
    foreach($user->follower_ids as $followerId) {//which now is array type
        echo $followerId;
    }
    if($user->isVirtualDirty('home_address')) {
        //detect virtual column is dirty or not 
        dd($user->getVirtualDirty());
    }

License

The MIT License (MIT). Please see License File for more information., (*11)

The Versions

23/05 2018