【问题标题】:How to make selection query for join tables in laravel?如何在 laravel 中对连接表进行选择查询?
【发布时间】:2018-05-24 00:39:36
【问题描述】:

我有用户和员工表。我想在登录后显示员工详细信息。人员显示将列出与登录用户相同部分的人员。例如,如果 Adam (section/seksyen=A) 登录,则显示的人员列表是 A 部门的人员。

如何在 laravel 中查询?

表用户:

  public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
        $table->string('section');
        $table->string('type');
    });
}

桌员:

 public function up()
{
    Schema::create('staffs', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->string('nama');
        $table->string('emel')->unique();
        $table->string('seksyen');
        $table->string('jawatan');
        $table->string('alamat');
        $table->string('no kakitangan');
    });

我试过这个查询,但没有用。

 $data['data'] = DB::table('staffs')
                 ->join('users', 'users.section', '=', 'staffs.seksyen')
                 ->where('seksyen','=','Auth(section)')->get();

【问题讨论】:

  • 究竟是什么不起作用?你真的在使用'Auth(section)'吗?

标签: php sql laravel-5


【解决方案1】:

提取用户登录部分名称的正确代码是:

        $section = Auth::user()->section;

        $data['data'] = DB::table('staffs')->where('section','=', $section)->get();

【讨论】:

    猜你喜欢
    • 2016-08-14
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多