【问题标题】:Getting data through pivot table using hasMany relationship使用 hasMany 关系通过数据透视表获取数据
【发布时间】:2016-01-28 17:29:13
【问题描述】:

您好,我有这三种型号:

用户.php

<?php namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Kodeine\Acl\Traits\HasRole;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword, HasRole;

    /**
     * The database table used by the model.
     *
     * @var stringSS
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password', 'is_active'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function roles()
    {
        return $this->belongsToMany('App\Models\Role', 'role_user', 'user_id', 'role_id');
    }

    public function bankBranch()
    {
        return $this->belongsToMany('App\Models\BankBranch', 'bank_branches_users', 'user_id', 'branch_id');
    }

    public function permissions()
    {
        return $this->belongsToMany('App\Models\Permissions', 'permission_user', 'user_id', 'permission_id');
    }

}

Bank.php

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Bank extends Model {

    protected $table = 'bank_details';

    public function branches()
    {
        return $this->hasMany('App\Models\BankBranch', 'bank_id');
    }

    public function users()
    {
        return $this->hasManyThrough('App\Models\User', 'App\Models\BankBranch');
    }

}

BankBranch.php

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class BankBranch extends Model {

    protected $table = 'bank_branches';

    public function bank()
    {
        return $this->belongsTo('App\Models\Bank', 'bank_id');
    }

    public function users()
    {
        return $this->hasMany('App\Models\User', 'bank_branches_users', 'branch_id', 'user_id');
    }

}

好的,现在在我的应用程序中,我有以下关系: 1. 用户属于多家银行分行。 2.银行分行属于一家银行。 3. BankBranch有很多用户。

现在,当用户登录时,我希望他们只能看到与该用户位于同一银行分行的其他用户。

在我的 admin->user 页面上的意思是,我想要一个与登录用户位于同一分支的用户列表。

除非登录用户属于许多其他分支,否则应该显示登录用户所属分支中的所有用户。

我在雄辩的模型中表示这一点并通过我的控制器获取数据时遇到了很大的困难。

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    获取用户bankBranches id:

    $branchIds = Auth::user()->bankBranch()->get()->lists('id');
    

    比获取属于该分支的所有用户:

    $usersInBranches = BankBranch::whereIn('id',$branchedIds)->with('users')->get();
    

    或:

    $usersInBranches = User::whereHas('bankBranch', function($query) use ($branchIds) {
     $query->whereIn('id',$branchIds);
    })->get();
    

    编辑甚至更好:

    $usersInBranches = User::whereHas('bankBranch', function($query){
     $query->whereIn('id',Auth::user()->bankBranch()->lists('id'));
    })->get();
    

    【讨论】:

    • 谢谢,但我不断收到完整性约束违规,字段列表中的“id”列不明确。请问我该如何解决这个问题?
    • 实际上都是。
    • 认为这与此有关:Auth::user->bankBranch()->lists('id')
    • Auth::user->bankBranch()->get()->lists("id");这份工作?
    猜你喜欢
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2018-06-08
    相关资源
    最近更新 更多