【问题标题】:Laravel Has undefined contstant App\User from tinkerLaravel 有来自 tinker 的未定义常量 App\User
【发布时间】:2021-07-17 11:27:06
【问题描述】:

我希望创建名为 laravel artisan tinker 的模型。

所以我尝试执行 php tinker .. 但是 App\User 命令发生错误。

我不知道。我该怎么办?

我的开发环境 流浪汉 php 8.* laravel 8.*

enter image description here enter image description here

>>> $user = App\User::create (['name' => 'rin', 'email' => 'rin@test.com', 'password' => bcrypt('123456')]);
PHP Error:  Class "App\User" not found in Psy Shell code on line 1
 
>>> App\User::find(1)->articles()->create(['title' => 'First article', 'content' => 'First content']);
PHP Error:  Class "App\User" not found in Psy Shell code on line 1

【问题讨论】:

  • 模型的默认命名空间在 Laravel 8 中更改为 App\Models

标签: php laravel eloquent laravel-artisan tinker


【解决方案1】:

在 Laravel 8 中,所有模型默认位于 Models 文件夹中。

所以你需要用App\Models\User替换App\User

文档:https://laravel.com/docs/8.x/structure#the-models-directory

【讨论】:

    【解决方案2】:

    只需使用:

    >>> $user = User::create ...
    
    

    tinker 将为您实例化一个 App\Models\User 模型。

    【讨论】:

      猜你喜欢
      • 2015-06-17
      • 1970-01-01
      • 2020-06-23
      • 2020-01-18
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 2020-12-29
      相关资源
      最近更新 更多