Skip to content

Commit

Permalink
Fixed creating default entries in the database
Browse files Browse the repository at this point in the history
  • Loading branch information
tylercd100 committed Sep 18, 2017
1 parent 0dd1805 commit 73bb90a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `laravel-licenser` will be documented in this file.

### 3.2.1
- Fixed creating default entries in the database

### 3.2.0
- Added an 'success' function to License

Expand Down
20 changes: 16 additions & 4 deletions src/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,26 @@ function __construct(Model $owner)
throw new LicenseException("The owner must use the trait: ".HasLicenses::class);
}

$this->model = $this->getModel($owner);
$this->owner = $owner;
$this->model = LicenseModel::firstOrCreate([
}

protected function getModel(Model $owner)
{
$opts = [
"owner_type" => get_class($owner),
"owner_id" => $owner->id,
"license" => get_class($this),
], [
"quantity" => $this->default,
]);
];

$x = LicenseModel::where($opts)->first();

if (!$x) {
$opts["quantity"] = $this->default;
$x = LicenseModel::create($opts);
}

return $x;
}

/**
Expand Down

0 comments on commit 73bb90a

Please sign in to comment.