23 lines
763 B
PHP
23 lines
763 B
PHP
<?php
|
|
|
|
namespace App\Models\Concerns;
|
|
|
|
use App\Models\Channel;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
trait HasChannelProvenance
|
|
{
|
|
public static function bootHasChannelProvenance(): void
|
|
{
|
|
static::saving(function (Model $model): void {
|
|
foreach (['channel_id', 'import_channel_id', 'origin_channel_id'] as $field) {
|
|
$id = $model->getAttribute($field);
|
|
if ($id !== null && ! Channel::withoutGlobalScope('tenant')->whereKey($id)->where('property_id', $model->property_id)->exists()) {
|
|
throw ValidationException::withMessages([$field => 'Der Kanal muss zur selben Wohnung gehören.']);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|