some work on models

This commit is contained in:
2018-07-14 14:34:17 +02:00
parent 6fd1d7eb89
commit 7493d3e176
7 changed files with 138 additions and 0 deletions
+31
View File
@@ -3,14 +3,21 @@
namespace App\Models;
use AdamWathan\EloquentOAuth\OAuthIdentity;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
/**
* A user in the application
*
* @property Proposal[]|Collection $proposals
*/
class User extends Authenticatable
{
use Reportable;
use Notifiable;
/**
@@ -31,6 +38,20 @@ class User extends Authenticatable
'password', 'remember_token',
];
protected static function boot()
{
parent::boot();
static::deleting(function(User $self) {
// manually delete proposals to ensure row refcounts are updated
foreach ($self->proposals as $proposal) {
$proposal->delete();
}
$self->notificationsCaused()->delete();
});
}
/** Owned tables */
public function dataTables()
{
@@ -78,4 +99,14 @@ class User extends Authenticatable
{
return $this->hasMany(Notification::class);
}
/**
* Notifications caused by this user
*
* @return HasMany
*/
public function notificationsCaused()
{
return $this->hasMany(Notification::class, 'actor_id');
}
}