You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

36 lines
1015 B

<?php
namespace App\Http\Middleware;
use App\Documents\ModelDocument;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class BindModelDocumentMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
$name = empty($request->route('resource_name')) ?
Str::of(explode('.', $request->route()->getName())[1])->singular()->value() :
$request->route('resource_name');
$modelDocument = ModelDocument::where('name', $name)
->with('storePolicy', 'updatePolicy', 'getPolicy', 'deletePolicy', 'restorePolicy')->first();
app()->bind('modelDocument', fn() => $modelDocument);
return $next($request);
}
}