Skip to content

Commit

Permalink
addd row dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
BSN4 committed Mar 30, 2020
1 parent 36b5c4c commit 8bb9feb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
},
"require-dev": {
"doctrine/dbal": "^2.5",
"phpunit/phpunit": "^8.3",
"fzaninotto/faker": "^1.6",
"guzzlehttp/guzzle": "^6.5",
"mockery/mockery": "^1.2",
"orchestra/testbench": "^3.5 || ^3.6 || ^3.7 || ^3.8 || ^4.0 || ^5.0"
"orchestra/testbench": "^3.5 || ^3.6 || ^3.7 || ^3.8 || ^4.0 || ^5.0",
"phpunit/phpunit": "^8.3"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 8 additions & 0 deletions src/Relationships/HasManyCached.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ public function getResults()
return $this->related::where($this->foreignKey,
$this->parent->{$this->ownerKey});
}

public function initRelation(array $models, $relation)
{
}

public function match(array $models, Collection $results, $relation)
{
}
}
30 changes: 25 additions & 5 deletions src/Traits/Kabsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@

trait Kabsa
{
/**
* @var Collection
*/
protected static $kabsaCollection;


public static function bootKabsa()
{
self::unguard();
}

public function getRows()
{
return $this->rows;
Expand All @@ -19,11 +27,23 @@ public static function all($columns = [])
if(!empty(static::$kabsaCollection)) {
return static::$kabsaCollection;
}

self::unguard();
$self = new self();

return static::$kabsaCollection = Collection::make($self->getRows() ?? [])->map(function ($row) { return new self($row); });
return static::$kabsaCollection = Collection::make(
(new static)->getRows() ?? [])
->map(function ($row) {
return new static($row);
});
}

public static function addRow($row)
{
if(static::$kabsaCollection instanceof Collection) {
static::$kabsaCollection->push(new static($row));
} else {
static::$kabsaCollection = Collection::make([new static($row)]);
}

return true;
}

public function __call($method, $parameters)
Expand Down
3 changes: 2 additions & 1 deletion tests/KabsaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Redis;
use Orchestra\Testbench\TestCase;

Expand Down Expand Up @@ -165,4 +166,4 @@ public function role()
{
return $this->belongsToKabsaRow(Role::class, 'label', 'role_label');
}
}
}

0 comments on commit 8bb9feb

Please sign in to comment.