46 lines
1.1 KiB
PHP
46 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*/
|
|
public function register(): void
|
|
{
|
|
$this->app->singleton('admin.menu', \App\Admin\Menu::class);
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*/
|
|
public function boot()
|
|
{
|
|
JsonResource::withoutWrapping();
|
|
|
|
$this->definePolymorphicTypes();
|
|
|
|
Validator::extend('phone', function ($attribute, $value, $parameters, $validator) {
|
|
return preg_match('/^1[\d]{10}$/', $value);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 自定义多态类型
|
|
*/
|
|
protected function definePolymorphicTypes(): void
|
|
{
|
|
Relation::enforceMorphMap(
|
|
collect([
|
|
\App\Models\AdminUser::class,
|
|
])->mapWithKeys(fn ($model) => [(new $model)->getTable() => $model])->all()
|
|
);
|
|
}
|
|
}
|