-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper methods
updateWithUserstamps
, deleteWithUserstamps
- Loading branch information
1 parent
892a158
commit 4d15ebc
Showing
5 changed files
with
147 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Wildside\Userstamps; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Scope; | ||
|
||
class UserstampsScope implements Scope | ||
{ | ||
/** | ||
* Apply the scope to a given Eloquent query builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return void | ||
*/ | ||
public function apply(Builder $builder, Model $model) | ||
{ | ||
return $builder; | ||
} | ||
|
||
/** | ||
* Extend the query builder with the needed functions. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @return void | ||
*/ | ||
public function extend(Builder $builder) | ||
{ | ||
$builder->macro('updateWithUserstamps', function (Builder $builder, $values) { | ||
if (! $builder->getModel()->isUserstamping() || is_null(auth()->id())) { | ||
return $builder->update($values); | ||
} | ||
|
||
$values[$builder->getModel()->getUpdatedByColumn()] = auth()->id(); | ||
|
||
return $builder->update($values); | ||
}); | ||
|
||
$builder->macro('deleteWithUserstamps', function (Builder $builder) { | ||
if (! $builder->getModel()->isUserstamping() || is_null(auth()->id())) { | ||
return $builder->delete(); | ||
} | ||
|
||
$builder->update([ | ||
$builder->getModel()->getDeletedByColumn() => auth()->id(), | ||
]); | ||
|
||
return $builder->delete(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters