php 找不到“App\Http\Controllers\DB”类,我也无法使用新模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26966652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class 'App\Http\Controllers\DB' not found and I also cannot use a new Model
提问by Peter
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.
我有非常基本的问题。在 L4 中,以下方法开箱即用,所以现在我迷路了。请帮忙。几天前,我开始了一个 Laravel 5.0 项目。我现在有了全新的、干净的安装。
Problem 1: When I try to get anything from database
问题 1:当我尝试从数据库中获取任何内容时
$headquote = DB::table('quotation_texts')->find(176);
I get this:
我明白了:
Class 'App\Http\Controllers\DB' not found
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:
问题 2:在克隆 User.php 模型之前,将类名更改为“引用”。下面是 App 根目录下的 Quotations.php 文件内容:
<?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';
}
Any attempt to use the model
任何使用模型的尝试
$headquote = Quotation::find(176);
ends up with this:
最终得到这个:
Class 'App\Http\Controllers\Quotation' not found
Any ideas how I could resolve the issue?
有什么想法可以解决这个问题吗?
回答by Marcin Nabia?ek
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).
这里的问题是 PHP 命名空间。您需要学习如何使用它们。由于您的控制器在App\Http\Controllers
命名空间中,如果您引用任何其他类,则需要use
在文件开头(类定义之前)添加前导反斜杠(或适当的命名空间)或添加语句。
So in your case you could use:
因此,在您的情况下,您可以使用:
$headquote = \DB::table('quotation_texts')->find(176);
$headquote = \App\Quotation::find(176);
or add in your controller class use
statement so the beginning of your controller class could look like this:
或添加您的控制器类use
语句,以便您的控制器类的开头看起来像这样:
<?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 PHPor namespaces in PHP manual
有关命名空间的更多信息,您可以查看How to use objects from other namespaces and how to import namespaces in PHP或namespaces in PHP manual
回答by CONvid19
Quick and dirty
又快又脏
use DB;
OR
或者
\DB::table...
回答by Mamun Rasid
Just add this top of your controller.
只需添加控制器的顶部即可。
use DB;
回答by Marvin Mustafa
Use the backslash before db on the header and you can use it then typically as you wrote it before.
在标题上的 db 之前使用反斜杠,然后您通常可以像之前编写的那样使用它。
Here is the example:
这是示例:
Use \DB;
Then inside your controller class you can use as you did before, like that ie :
然后在您的控制器类中,您可以像以前一样使用,例如:
$item = DB::table('items')->get();
回答by radhason power
Try Like this:
试试这样:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class UserController extends Controller
{
function index(){
$users = DB::table('users')->get();
foreach ($users as $user)
{
var_dump($user->name);
}
}
}
?>
回答by infantry
I like to do this witch i think is cleaner :
我喜欢做这个我认为更干净的女巫:
1 - Add the model to namespace:
1 - 将模型添加到命名空间:
use App\Employee;
2 - then you can do :
2 - 然后你可以这样做:
$employees = Employee::get();
or maybe somthing like this:
或者可能是这样的:
$employee = Employee::where('name', 'John')->first();
回答by vishal
There is problem in name spacing as in laravel 5.2.3
Laravel 5.2.3 中的名称间距存在问题
use DB;
use App\ApiModel; OR use App\name of model;
DB::table('tbl_users')->insert($users);
OR
DB::table('table name')->insert($users);
model
class ApiModel extends Model
{
protected $table='tbl_users';
}