Skip to content

Commit

Permalink
Fixed relationship validation error
Browse files Browse the repository at this point in the history
  • Loading branch information
Flobbo authored and Flobbo committed Dec 22, 2017
1 parent fedcad2 commit e5992ea
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Crudable/Crudable.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ public function orderBy($field, $order = 'asc'){
public function create(array $data){
$model = $this->model->create($data);
//check for hasMany
if($this->validateRelationData($this->withHasMany)){
if($this->validateRelationData($this->withHasMany,'many')){
$model->{$this->withHasMany['relation']}()->saveMany($this->withHasMany['data']);
}
//check for belongsToMany
if($this->validateRelationData($this->withBelongsToMany)){
if($this->validateRelationData($this->withBelongsToMany,'tomany')){
$model->{$this->withBelongsToMany['relation']}()->sync($this->withBelongsToMany['data']);
}
return $model;
Expand Down Expand Up @@ -198,17 +198,19 @@ public function handleUpload(\Illuminate\Http\Request $request, $fieldname = 'ph
return $filename;
}

private function validateRelationData($related_data){
private function validateRelationData($related_data, $type){
//Check if data attribute was set
if(!is_null($this->withHasMany)){
if(!is_null($this->withHasMany) && $type == 'many'){
if(!isset($this->withHasMany['relation']) || !isset($this->withHasMany['data']))
throw new MissingRelationDataException('HasMany Relation');
return true;
}
if(!is_null($this->withBelongsToMany)){
if(!is_null($this->withBelongsToMany) && $type == 'tomany'){
if(!isset($this->withBelongsToMany['relation']) || !isset($this->withBelongsToMany['data']))
throw new MissingRelationDataException('HasMany Relation');
return true;
}
return true;
return false;
}

}

0 comments on commit e5992ea

Please sign in to comment.