对于员工管理系统,我使用laravel 5查询生成器时遇到问题。这里是我的EmployeesController在Laravel 5控制器中找不到类'App Http Controllers DB'
<?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'));
}
}
当我使用注释掉的代码,该视图的作品,我可以看到我的员工列表
$employees = Employee::all();
return view('employees.index', compact('employees'));
我看到了一个answer在这里,我也的建议,但没有运气。我添加了使用数据库;命名空间声明后,也试图与
$employees = \DB::table('employees')->get();
的代码,但它抛出另一个错误,说调用一个成员函数count()非物体上线6 我甚至复制的db.php中的文件从C:\ xampp \ htdocs \ laravel5project \ vendor \ laravel \ framework \ src \ Illuminate \ Support \ Facades到App文件夹(C:\ xampp \ htdocs \ laravel5project \ app),但仍然没有运气。 我也想明确地给它的命名空间
use Illuminate\Support\Facades\DB
下面是这个视图
@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
可能是什么问题呢?
你要在哪里使用' - > count()'?我没有在你的代码中看到它。另外,如果你尝试在'null'对象上调用' - > count()',你会得到这个错误。此外,使用'\ DB'将摆脱标题中的错误。这是通过使用\解决的命名空间问题。不幸的是,之后你遇到了另一个错误,但它们有些不相关。 –
刚刚包括在问题上的观点 – user3659497
那么,你是对的。这是观点上的计数函数给我带来了问题。 – user3659497