在 Laravel 5 控制器中找不到类“App\Http\Controllers\DB”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33106904/
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 in Laravel 5 Controller
提问by user3659497
I have a problem using the laravel 5 query builder for an Employee management system. Here is my EmployeesController
我在为员工管理系统使用 laravel 5 查询构建器时遇到问题。这是我的员工控制器
<?php
namespace App\Http\Controllers;
use App\Employee;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class EmployeesController extends Controller
{
public function index()
{
// $employees = Employee::all();
// return view('employees.index', compact('employees'));
$employees = DB::table('employees')->get();
return view('employees.index', compact('employees'));
}
}
When i use the commented out code, the view works and i can see my employee list
当我使用注释掉的代码时,视图工作,我可以看到我的员工列表
$employees = Employee::all();
return view('employees.index', compact('employees'));
I saw an answerhere, and i did as suggested but no luck. I added use DB; after the namespace declaration and also tried the code with
我在这里看到了一个答案,我按照建议做了,但没有运气。我添加了使用数据库;在命名空间声明之后,还尝试了代码
$employees = \DB::table('employees')->get();
but it throws another error which says Call to a member function count() on a non-object on line 6. I even copied the DB.php file from C:\xampp\htdocs\laravel5project\vendor\laravel\framework\src\Illuminate\Support\Facades to the App folder (C:\xampp\htdocs\laravel5project\app) but still no luck. I've also tried to explicitly give it the namespace
但它引发了另一个错误,它说在第 6 行的非对象上调用成员函数 count()。我什至从 C:\xampp\htdocs\laravel5project\vendor\laravel\framework\src\ 复制了 DB.php 文件Illuminate\Support\Facades 到 App 文件夹 (C:\xampp\htdocs\laravel5project\app) 但仍然没有运气。我也试图明确地给它命名空间
use Illuminate\Support\Facades\DB
Here is the view
这是视图
@extends('layouts.default')
@section('PageTitle', 'Employee List')
@section('content')
@if ( !$employees->count() )
There are no Employees!
@else
<table id="tblEmployee" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
@foreach( $employees as $employee )
<tr>
<td>{{$employee->Name}}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
@endsection
What could be the problem?
可能是什么问题呢?
回答by mniess
DB
is not in your current namespace App\Http\Controllers
. So you can either import it at the top
DB
不在您当前的命名空间中App\Http\Controllers
。所以你可以在顶部导入它
use DB;
or precede it with a backslash \DB::table(...)
. This solves the class not found exception.
或在它前面加上反斜杠\DB::table(...)
。这解决了找不到类的异常。
You are however not getting a Laravel Collection of Employee models but an array of database rows. Arrays are not objects that have a count() function which results in your final error.
但是,您得到的不是 Laravel 员工模型集合,而是数据库行数组。数组不是具有导致最终错误的 count() 函数的对象。
Update: Laravel 5.3 will return a Collection object and not an array. So count() will work on that.
更新:Laravel 5.3 将返回一个 Collection 对象而不是一个数组。所以 count() 会起作用。
回答by Khawar Hussain
The DB is not set in your Controller:
数据库未在您的控制器中设置:
<?php
namespace App\Http\Controllers;
use DB;
use App\Employee;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class EmployeesController extends Controller
{
public function index()
{
// $employees = Employee::all();
// return view('employees.index', compact('employees'));
$employees = DB::table('employees')->get();
return view('employees.index', compact('employees'));
}
}