laravel - 在路由回调函数中使用 Eloquent 模型会出错

标签 laravel eloquent

为什么我不能在 routes.php 中使用 Eloquent 模型。

像这样

Route::get('image', function(){
    $user= [
        "image_name" => 'one'
    ];
    User::create($user);
});

我还尝试了 use App\Users;namespace App 它给出了错误。

在深入研究 Controller 之前,我需要检查路由本身的一些内容。 如何在 routes.php 中实现这项工作?

最佳答案

你需要添加

use App\User;

Routes.php 的顶部

注意:App\User,而不是App\Users

关于laravel - 在路由回调函数中使用 Eloquent 模型会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33722991/

相关文章:

laravel - DomPDF 在本地主机中的行为与 GoDaddy 托管不同

php - 如果电子邮件未通过验证,如何限制 Laravel 中的用户登录

database - Laravel - 在验证唯一性时从登录用户中排除该行

php - 是否可以使用 Eloquent 方式为模型获取不同模型类型的集合?

mysql - 如何将数字枚举列转换为tinyint?

mysql - Eloquent 中一个查询的多个求和

php - Laravel Eloquent : how to use “where” with WithCount to select Models whose count is larger than a number

javascript - 如何通过应用程序注入(inject)自定义js到shopify

javascript - axios POST 后在 Vue 组件中显示更新数据的问题

mysql - Laravel Eager Loading 两端都有一些 where 条件