【发布时间】:2016-03-07 20:53:31
【问题描述】:
使用 Laravel 5.2,我在 db 表“users”中添加了一列“code”,我想在控制器中过滤 DB::table 结果。
我在视图中渲染行很好。但是,当我添加 where 子句 ->where('code', $codevar) 时,你会收到一条 Auth not found 的错误消息。如何
表用户
id email code password
-------+----------------------------+------------+-------------------
1 admin@admin.com (logged) 007 $2y$10$azWc9kKFU/...
2 user@user.com 666 $2y$10$azWc9kKFU/...
报告表
code date price
-------+------------------+--------
007 2016-01-20 $10.20
666 2016-02-22 $58.00
输出
code month price
-------+------------------+--------
007 01 $10.20
666 02 $58.00
预期输出
code month price
-------+------------------+--------
007 01 $10.20
MyController.php
<?php
use DB;
use Auth;
namespace LaravelAcl\Authentication\Controllers;
use Illuminate\Http\Request;
use LaravelAcl\Library\Form\FormModel;
use LaravelAcl\Authentication\Models\Permission;
use LaravelAcl\Authentication\Validators\PermissionValidator;
use LaravelAcl\Library\Exceptions\JacopoExceptionsInterface;
use View, Redirect, App, Config;
class ResumenesController extends Controller
{
public function getList()
{
$user = \Auth::user();
$codevar = \Auth::user()->code;/// maybe like this
$rows= \DB::table('reports')
->select('*', \DB::raw('MONTH(date) as month)')
->where('code', $codevar)*
->orderBy('month','DESC')
->paginate(15);
return View::make('client.report.list', compact('rows','codevar', 'user'));
}
我如何检索 \Auth::user() 或者 Auth::user()->code。 只有我想为过滤表结果获取这些额外的用户表列...
有什么想学的吗?
【问题讨论】:
-
使用文件顶部的 use 语句导入类后,无需全局引用 Auth 类。尝试从 getList 方法的前两行中删除反斜杠。
-
谢谢克里斯。如果删除显示错误 Auth not found 或 Db not found.
标签: php mysql laravel eloquent