Skip to content

Commit

Permalink
Avoid model being null when mutator doesnt return anything
Browse files Browse the repository at this point in the history
- Fix #231
- Added exception that warns about #211
  • Loading branch information
santigarcor committed Dec 18, 2017
1 parent e523166 commit 3ce89ac
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Support\Facades\Config;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Laratrust\Contracts\LaratrustTeamInterface;

class Helper
{
Expand Down Expand Up @@ -162,10 +161,12 @@ public static function hidrateModel($class, $data)
return $data;
}

$model = (new $class)
->setAttribute('id', $data['id'])
->setAttribute('name', $data['name']);
if (!isset($data['pivot'])) {
throw new \Exception("The 'pivot' attribute in the {$class} is hidden");
}

$model = new $class;
$model->setAttribute('id', $data['id'])->setAttribute('name', $data['name']);
$model->setRelation(
'pivot',
MorphPivot::fromRawAttributes($model, $data['pivot'], 'pivot_table')
Expand Down

0 comments on commit 3ce89ac

Please sign in to comment.