<?php

namespace App\Http\Middleware;

use App\Models\Project;
use Closure;
use Illuminate\Http\Request;

class BindProjectModel
{
    public function handle(Request $request, Closure $next)
    {
        $route = $request->route();
        if ($route && $route->parameter('project')) {
            $projectId = $route->parameter('project');
            if (is_string($projectId) && is_numeric($projectId)) {
                $project = Project::findOrFail($projectId);
                $route->setParameter('project', $project);
            }
        }

        return $next($request);
    }
}
