2018-11-15 10:45:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2018-11-15 10:45:49 +00:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
2021-06-20 13:03:20 +00:00
|
|
|
use Laravel\Passport\Passport;
|
2018-11-15 10:45:49 +00:00
|
|
|
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
|
|
{
|
2021-05-22 14:38:19 +00:00
|
|
|
/**
|
|
|
|
* Register any application services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
2021-06-20 13:03:20 +00:00
|
|
|
Passport::ignoreMigrations();
|
2021-05-22 14:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bootstrap any application services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function boot()
|
|
|
|
{
|
|
|
|
// When a query should return 1 object, or FAIL if it doesnt
|
|
|
|
Builder::macro('singleOrFail',function () {
|
|
|
|
$result = $this->get();
|
|
|
|
|
|
|
|
if (($x=$result->count()) == 1) {
|
|
|
|
return $result->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \Exception(sprintf('Query brings back %d record(s) called for singleOrFail()',$x));
|
|
|
|
});
|
|
|
|
|
|
|
|
// When a query should return 1 object, or NULL if it doesnt
|
|
|
|
Builder::macro('single',function () {
|
|
|
|
$result = $this->get();
|
|
|
|
|
|
|
|
if ($result->count() == 1) {
|
|
|
|
return $result->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
});
|
|
|
|
}
|
2018-11-15 10:45:49 +00:00
|
|
|
}
|