Skip to content

Commit

Permalink
fix make on hasMany
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhall17 committed May 29, 2020
1 parent 67cb418 commit 9f8bae6
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/Support/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ public function __construct($related, $owner, $name, $field, $updateFields = [])
$this->boot();
}

protected function setUpdateFields($fields)
protected function setUpdateFields($fields)
{
$this->updateFields = $fields;
if($this->owner->hasKey()){
$this->updateFields[$this->field] = $this->owner->getKey();
}
}

public function hasUpdateFields()
public function hasUpdateFields()
{
return $this->updateFields != [];
}

public function getUpdateKeys()
public function getUpdateKeys()
{
return array_keys($this->updateFields);
}
Expand All @@ -56,7 +56,7 @@ public function updateFields($item){
return $item;
}

public function boot()
public function boot()
{
if(array_key_exists($this->name, $this->owner->getAttributes())){
$this->hydrate($this->owner->getAttributes()[$this->name]);
Expand All @@ -75,7 +75,7 @@ public function boot()
}
}

protected function hydrate($array)
protected function hydrate($array)
{
$collection = new Collection;
if($array != []){
Expand All @@ -86,15 +86,15 @@ protected function hydrate($array)
$this->relation = $collection;
}

public function empty()
public function empty()
{
$this->relation = new Collection;
return $this;
}

public function make($data)
{
$this->attach($this->newRelation($this->updateFields($data)));
$this->attach(($object = $this->newRelation($this->updateFields($data))));
return $object;
}

Expand All @@ -106,7 +106,7 @@ public function attach($object)

public function detach($object)
{

}

public function save(object $object)
Expand Down Expand Up @@ -140,25 +140,25 @@ public function createMany(array $data)
return $this;
}

public function getResults()
public function getResults()
{
if($this->relation->count() == 0){
$this->getRelationFromApi();
}
}
return $this->relation;
}

public function first()
public function first()
{
return $this->getResults()->first()->fresh();
}

public function last()
public function last()
{
return $this->getResults()->last()->fresh();
}

public function getRelationFromApi()
public function getRelationFromApi()
{
$this->relation = $this->newRelation($this->updateFields([]))->setPassOnAttributes($this->getUpdateKeys())->all();
if($this->hasUpdateFields()){
Expand All @@ -169,7 +169,7 @@ public function getRelationFromApi()
return $this;
}

public function nextPage()
public function nextPage()
{
$this->relation = $this->relation->nextPage();
if($this->hasUpdateFields()){
Expand All @@ -179,7 +179,7 @@ public function nextPage()
}
}

public function prevPage()
public function prevPage()
{
$this->relation = $this->relation->prevPage();
if($this->hasUpdateFields()){
Expand All @@ -203,9 +203,9 @@ public function __call($method, $parameters)
} else {
$relation = $this->newRelation($this->updateFields([]))->setPassOnAttributes($this->getUpdateKeys());
return $this->forwardCallTo($relation, $method, $parameters);
}
}
}

// Be good to add these:- findOrNew, firstOrNew, firstOrCreate and updateOrCreate

}
}

0 comments on commit 9f8bae6

Please sign in to comment.