【问题标题】:Laravel 5 Route Model Binding not working on serverLaravel 5 路由模型绑定在服务器上不起作用
【发布时间】:2015-04-24 13:43:45
【问题描述】:

我对 Laravel 5 路由模型绑定有疑问 我正在使用以下控制器方法

public function destroy(PrimaryLocation $primaryLocation) {
    dd($primaryLocation->id);
    $primaryLocation->delete();
    return redirect()->back()->with('locationDeleted', true);
}

PrimaryLocation 是一个 Eloquent 模型

我的RouteServiceProvider的开机功能:

public function boot(Router $router)
{
    parent::boot($router);

    $router->model('user', 'App\User');
    $router->model('PrimaryLocation', 'App\PrimaryLocation');
}

在我的 routes.php 中

Route::delete('deletePrimaryLocation/{PrimaryLocation}',
              ['as' => 'admin.deletePrimaryLocation', 'uses' => 'LocationsController@destroy']);

此设置在我的本地计算机上运行良好,但是当我将文件部署到我的开发服务器时,模型绑定会中断; 执行该方法时,位置不会被删除。

我做了一些 var_dumps

dd($primaryLocation->id); 

在本地计算机上这会返回正确的 id,但在服务器上它会 只返回null;

但是如果我做一个

dd($primaryLocation)

结果在本地

    PrimaryLocation {#178 ▼
    #fillable: array:1 [▶]
    #connection: null
    #table: null
    #primaryKey: "id"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: array:4 [▶]
    #original: array:4 [▶]
    #relations: []
    #hidden: []
    #visible: []
    #appends: []
    #guarded: array:1 [▶]
    #dates: []
    #casts: []
    #touches: []
    #observables: []
    #with: []
    #morphClass: null
    +exists: true
  }

在我的服务器上几乎相同...但缺少属性:

        PrimaryLocation {#195 ▼
  #fillable: array:1 [▶]
  #connection: null
  #table: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: []
  #original: []
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #guarded: array:1 [▶]
  #dates: []
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: false
}

有人知道可能出了什么问题吗?

[更新]

如果我注释掉

// $router->model('PrimaryLocation', 'App\PrimaryLocation');

在我的 ServiceProvider 中,本地行为与服务器上的相同。 加载 ServiceProvider 可能有问题?也许有某种缓存?

【问题讨论】:

  • 缺少attributes是因为找不到记录。数据库中的数据是否相同?即相同的数据库、表和字段?具体来说——记录本身是否存在?
  • 您的文件名在服务器上是否正确?是PrimaryLocation.php 区分大小写一样吗?
  • 如果找不到记录,应该有 404 错误(至少文档说明了这一点),如果我是正确的。如果文件名不正确,则应该是某种“硬”错误...(哎呀,出了点问题)
  • 是的 - 正确 - 但我只是想排除各种问题。
  • 只是为了澄清 - 问题是没有找到记录。我不知道为什么它没有抛出 404 - 但你得到 nullid 的事实证明了这一点。另外,当您dd() 时,您可以清楚地看到exists: false 的对象。所以我们需要确认记录是实际上在数据库中 - 然后从那里开始。

标签: laravel laravel-5


【解决方案1】:

在经历了同样的问题后,我发现在生产环境中,storage/framework/compiled.php 不会像在开发模式中那样定期重建。

基本上,您只是在生产服务器上运行旧版本的 RoutesServiceProvider.php。

不过,修复很简单。只需运行php artisan clear-compiled

在任何部署脚本中添加行也是一个好习惯。

【讨论】:

    【解决方案2】:

    只需将这一行添加到 serviceProvider::boot()

     $router->model('attribute', Attribute::class);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 2015-07-12
      • 2018-02-28
      • 2016-04-16
      • 2018-06-11
      • 2020-10-03
      • 1970-01-01
      相关资源
      最近更新 更多