找不到类"App \ Http \ Controllers \ DB",我也无法使用新模型 [英] Class 'App\Http\Controllers\DB' not found and I also cannot use a new Model

查看:505
本文介绍了找不到类"App \ Http \ Controllers \ DB",我也无法使用新模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的问题.在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 'App\Http\Controllers\DB' not found

问题2:在克隆User.php模型之前,将类名称更改为"Quotation".以下是放在应用程序根文件夹中的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 Illuminate\Database\Eloquent\Model;

 class Quotation extends Model  {

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

任何尝试使用模型

$headquote = Quotation::find(176);

最后是这样:

Class 'App\Http\Controllers\Quotation' not found

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

Any ideas how I could resolve the issue?

推荐答案

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

The problem here are PHP namespaces. You need to learn how to use them. As your controller are in App\Http\Controllers 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).

因此,您可以使用:

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

或添加控制器类use语句,以便控制器类的开始看起来像这样:

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

<?php

namespace App\Http\Controllers;

use DB;
use App\Quotation;

有关名称空间的更多信息,请参见

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

这篇关于找不到类"App \ Http \ Controllers \ DB",我也无法使用新模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆