找不到类“AppHttpControllersDB",我也无法使用新模型

2022-01-14 00:00:00 namespaces php laravel laravel-5 models

我有一个非常基本的问题.在 L4 中,以下方法是开箱即用的,所以现在我迷路了.请帮忙.几天前我开始了一个 Laravel 5.0 项目.我现在有全新、干净的安装.

I have very basic problem. In L4 thes below methods worked out of the box, so now I am lost. Please help. A few days ago I started a Laravel 5.0 project. I have now fresh, clean installation.

问题 1:当我尝试从数据库中获取任何内容时

Problem 1: When I try to get anything from database

$headquote = DB::table('quotation_texts')->find(176);

我明白了:

Class 'AppHttpControllersDB' not found

问题 2:在我克隆 User.php 模型之前,将类名更改为引用".以下是 App 根目录下的 Quotations.php 文件内容:

Problem 2: Before I cloned the User.php Model, changed Class name to "Quotation". Below is the content of file Quotations.php put in App root folder:

<?php namespace App;

 use IlluminateDatabaseEloquentModel;

 class Quotation extends Model  {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'quotation_texts';
}

任何使用模型的尝试

$headquote = Quotation::find(176);

这样结束:

Class 'AppHttpControllersQuotation' not found

有什么想法可以解决这个问题吗?

Any ideas how I could resolve the issue?

推荐答案

这里的问题是 PHP 命名空间.你需要学习如何使用它们.由于您的控制器位于 AppHttpControllers 命名空间中,如果您引用任何其他类,则需要添加前导反斜杠(或适当的命名空间)或添加 use 语句文件的开头(在类定义之前).

The problem here are PHP namespaces. You need to learn how to use them. As your controller are in AppHttpControllers namespace, if you refer any other class, you need to add leading backslash (or proper namespace) or add use statement at the beginning of file (before class definition).

所以在你的情况下你可以使用:

So in your case you could use:

$headquote = DB::table('quotation_texts')->find(176);
$headquote = AppQuotation::find(176);

或在你的控制器类中添加 use 语句,这样你的控制器类的开头可能如下所示:

or add in your controller class use statement so the beginning of your controller class could look like this:

<?php

namespace AppHttpControllers;

use DB;
use AppQuotation;

有关命名空间的更多信息,您可以查看 如何使用来自其他命名空间的对象以及如何在 PHP 中导入命名空间 或 PHP 手册中的命名空间

For more information about namespaces you could look at How to use objects from other namespaces and how to import namespaces in PHP or namespaces in PHP manual

相关文章