【问题标题】:Laravel: Eloquent / Tinker Class not foundLaravel:Eloquent / Tinker Class not found
【发布时间】:2017-08-04 19:58:05
【问题描述】:

我正在尝试获取与 tinker 的关系中的数据。我收到一条错误消息,提示找不到 Class 'App\Date'。我想是因为我没有使用正确的代码,但我不知道我需要什么代码。

我有 2 个模型:

联系人 (app\Models\Contact.php)
日期 (app\Models\Date.php)

联系方式

public function dates(){
   return $this->hasMany(Date::class);
}

修补匠

php artisan tinker

$test = App\Models\Contact::find(516);

$test 返回一个用户。
如果我使用 $test->dates 之后我会收到一条错误消息:

[Symfony\Component\Debug\Exception\FatalErrorException]Class 'App\Date' not found

【问题讨论】:

  • 请显示您的联系人型号。主要是关系部分。
  • 关系部分已经显示,它的公共函数dates()
  • 使用return $this->hasMany(App\Models\Date, 'contact_id');
  • 在你的关系中尝试完整的命名空间。 App\Models\Date::class

标签: php laravel eloquent


【解决方案1】:

确保您在Date.php 中使用了正确的命名空间,它应该是namespace App\Models; 而不是namespace App;

【讨论】:

  • 我正在使用 App\Models;作为我的 Date.php 中的命名空间,仍然无法工作
  • @KoenvandeSande 确保您也在 Contact 类中使用正确的命名空间并尝试运行 composer dumpauto 命令
【解决方案2】:

$test->dates 很可能是一种关系。在这种关系中,它无法找到关系中使用的类。

public function dates(){
   return $this->hasMany('App\Models\Date');
}

【讨论】:

  • 我想我需要说明 Date 类在 app\Models\ 中的关系?这可能吗?
  • 你使用的是什么版本的 Laravel?如果模型在同一个命名空间中,你的代码应该可以工作。
  • 我正在使用 Laravel 5。
  • 我已经重新安装了 phpstorm 和我的 ssd,由于某种原因它现在可以与您的代码一起使用。认为它与旧代码或其他东西有关。谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-11-06
  • 2018-08-22
  • 1970-01-01
  • 2015-02-12
  • 2023-04-03
  • 2018-09-26
  • 2018-06-27
  • 2013-10-05
相关资源
最近更新 更多