我已经没有主意了,所以我想我应该四处询问。这是我的代码:
# app/Map/MapServiceProvider.php
class MapServiceProvider extends ServiceProvider
{
/**
* Bootstrap any package services.
*/
public function boot(): void
{
Route::model('map_marker', Marker::class);
$this->loadMigrationsFrom(__DIR__.'/Migrations');
$this->loadRoutesFrom(__DIR__.'/routes.php');
}
// ...
}
提供商已添加到
app.php
'providers' => ServiceProvider::defaultProviders()->merge([
// ...
App\Map\MapServiceProvider::class,
])->toArray(),
# app/Map/routes.php
use App\Map\Controllers\UpdateMarkerController;
use Illuminate\Support\Facades\Route;
Route::put('/marker/{map_marker}', UpdateMarkerController::class)
->whereUuid('map_marker');
// ...
# app/Map/Migrations/.._create_map_markers_table.php
Schema::create('map_markers', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->double('lat');
$table->double('lng');
$table->timestamps();
});
# app/Map/Models/Marker.php
class Marker extends Model
{
use HasUuids;
protected $table = 'map_markers';
protected $fillable = ['lat', 'lng'];
}
模型似乎没有解决。当注入控制器的 invoke 方法时,我得到一个空模型:
# app/Map/Controllers/UpdateMarkerController.php
class UpdateMarkerController extends Controller
{
public function __invoke(Marker $marker): JsonResponse
{
dd($marker);
}
}
结果:
App\Map\Models\Marker {#285 // app/Map/Controllers/UpdateMarkerController.php:21
#connection: null
#table: "map_markers"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
+usesUniqueIds: true
#hidden: []
#visible: []
#fillable: array:2 [
0 => "lat"
1 => "lng"
]
#guarded: array:1 [
0 => "*"
]
}
当我尝试直接从路由器中拉出它时:
# app/Map/Controllers/UpdateMarkerController.php
class UpdateMarkerController extends Controller
{
public function __invoke(\Illuminate\Http\Request $request): JsonResponse
{
dd($request->route('map_marker'));
}
}
我只得到绑定段的值:
"9a783cd6-3e87-49b8-b392-e1bb8be45521"
我确信我一定错过了一些简单的东西。
问题出在路线上。它应该与您的型号名称相同。
尝试使用:
Route::put('/marker/{marker}', UpdateMarkerController::class)
此后您的模型将拾取它。
此外,您想要实现的目标不需要
whereUuid
。
UpdateMarkerController 中标记模型的模型绑定似乎可能存在问题。让我们看一下您的代码,看看是否可以找出问题所在。
确保routes.php中的路由定义中的路由参数命名正确。在您的代码中,您已将参数指定为map_marker:
Route::put('/marker/{map_marker}', UpdateMarkerController::class)
->whereUuid('map_marker');
在 UpdateMarkerController 中,您应该键入提示参数名称以匹配路由参数:
public function __invoke(Marker $map_marker): JsonResponse
{
dd($map_marker);
}
通过这样做,Laravel 应该使用 map_marker 路由参数自动解析 Marker 模型。
确保您的 Marker 模型正确使用 HasUuids 特征,并且 UUID 作为主键存储在数据库中。根据您的迁移,它似乎设置正确。 如果您已检查上述所有内容但仍然无法正常工作,请执行以下一些其他故障排除步骤:
确保您已使用 use App\Map\Models\Marker; 在 UpdateMarkerController 文件顶部导入了 Marker 模型。
验证您的模型、控制器和路由定义中没有拼写错误或命名差异。
通过运行 php artisan route:clear 清除路由缓存,以确保对路由所做的任何更改都被拾取。
确保您在路由中传递的 UUID 值与数据库中的现有记录匹配。