【问题标题】:Laravel 5.2 extra column in table UsersLaravel 5.2 用户表中的额外列
【发布时间】: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


【解决方案1】:

您可以使用 auth()-&gt;user() 全局帮助程序或 Auth 外观,但请记住,在检索经过身份验证的用户时,您将始终获得所有字段。

正如 Christian Gerdes 在评论中所说,在顶部导入 Auth 门面并删除反斜杠。对 DB 外观做同样的事情。

【讨论】:

    猜你喜欢
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 2017-01-13
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 2016-05-17
    相关资源
    最近更新 更多