Laravel编辑控制器没有数据

发布于 2025-02-03 02:45:59 字数 636 浏览 2 评论 0原文

我试图在拉拉维尔(Laravel)做烂。在编辑函数属性中执行数据变量的DD时,数组会获取NULL

路由

Route::resource('/gameSettings', GameSettingController::class);

控制器

public function edit(GameSetting $game_setting)
{
    dd($game_settings);
    return view('admin.game_setting.edit', compact('game_setting'));
}

模型

class GameSetting extends Model
{
    use HasFactory;
    protected $fillable = [
        'coin_value',
        'minimum_withdraw_amount'
    ];
}

链接

https://localhost:8000/admin/gameSettings/1/edit

DD($ GAGE_SETTINGS);给出空数组属性

I am trying to make crud in laravel. While doing dd of data variable in edit function attributes array is getting null

Route

Route::resource('/gameSettings', GameSettingController::class);

Controller

public function edit(GameSetting $game_setting)
{
    dd($game_settings);
    return view('admin.game_setting.edit', compact('game_setting'));
}

Model

class GameSetting extends Model
{
    use HasFactory;
    protected $fillable = [
        'coin_value',
        'minimum_withdraw_amount'
    ];
}

Link

https://localhost:8000/admin/gameSettings/1/edit

dd($game_settings); giving null array attribute

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2025-02-10 02:45:59

我没有足够的代表来发表评论,所以我给出答案...

@lagbox是正确的。您的路由参数应完全匹配您的案例控制器中的变量类型 to $ gamesetting

如果您想使用$ game_setting将路由更改为

Route::resource('/gameSettings', GameSettingController::class, ['parameters' => ['gameSetting' => 'game_setting']]);

I dont have enough rep to comment so I give an answer...

@lagbox is correct. Your route parameter should match exactly as the variable typehinted in the controller for your case change $game_setting to $gameSetting

if you want to use $game_setting change your route to

Route::resource('/gameSettings', GameSettingController::class, ['parameters' => ['gameSetting' => 'game_setting']]);
≈。彩虹 2025-02-10 02:45:59

您已经在控制器方法上使用的变量必须与您定义的路由参数的名称完全匹配。在这种情况下,参数将被命名为goometting很可能。如果您不匹配这些,那么您会发生依赖注入,这将为您提供一个新的,不存在的模型实例。如果您匹配名称,则将获得路由模型绑定,它将查找模型并为您提供该特定实体。

如果您想查看路由参数的命名,则由于使用资源路由,可以从命令行中运行PHP Artisan Route:list,它将向您显示这7个路由以及它们如何定义。

The variable that you have typehinted on the Controller method must match exactly the name of the route parameter you have defined. In this case the parameter would be named gameSetting most likely. If you don't match these then you have Dependency Injection happening which would give you a new, non-existing, instance of the model. If you match the name then you will get Route Model Binding and it will look up the model and give you that particular entity.

If you want to see what the route parameter is named, since you are using resource routing, you can run php artisan route:list from the command line and it will show you those 7 routes and how they are defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文