【问题标题】:Custom model binding when using withTrashed method on Route declaration and resolveRouteBinding in Model在 Route 声明上使用 withTrashed 方法并在 Model 中使用 resolveRouteBinding 时的自定义模型绑定
【发布时间】:2022-01-09 17:12:42
【问题描述】:

我在软删除模型路由上遇到隐式模型绑定问题。当使用withTrashed 方法时,Model 中的resolveRouteBinding 方法不会被调用。当withTrashed 方法从路由声明 中移除时,resolveRouteBinding 方法将按预期调用。

复制步骤:

路由/web.php

Route::get('user/{user}', function(SoftDeletedModel $user) {
    dd($user);
})->withTrashed(); //withTrashed is used

模型/SoftDeletedModel.php

use SoftDeletes;

public function resolveRouteBinding($value, $field = null) {
    dd("Successfully Substituted Bindings when using WithTrashed."); //This is not displayed.
    return parent::resolveRouteBinding($value, $field);
}

这是一个已知的错误还是我哪里出错了? github上还有一个issue

【问题讨论】:

    标签: php laravel eloquent laravel-routing


    【解决方案1】:

    你需要使用resolveSoftDeletableRouteBinding这个方法,而不是resolveRouteBinding

        /**
         * Retrieve the model for a bound value.
         *
         * @param mixed $value
         * @param string|null $field
         *
         * @return \Illuminate\Database\Eloquent\Model|null
         */
        public function resolveSoftDeletableRouteBinding($value, $field = null)
        {
            
             return parent::resolveSoftDeletableRouteBinding($value, $field);
        }
    

    【讨论】:

      猜你喜欢
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 2013-08-21
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多