【问题标题】:Count how many users are there per each category计算每个类别有多少用户
【发布时间】:2019-01-04 20:29:46
【问题描述】:

我正在尝试获取用户数据,他们将部门值放入数组中,以便我可以对其进行分组并计算有多少用户属于同一部门。

用户表:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();         
});

部门表:

Schema::create('departments', function (Blueprint $table) {
    $table->increments('id');
    $table->timestamps();
    $table->string('name');
});

分配表:

Schema::create('assignments', function (Blueprint $table) {
     $table->increments('id');
     $table->timestamps();
     $table->string('name');
     $table->unsignedInteger('user_id')->nullable();
     $table->foreign('user_id')->references('id')->on('users');   
     $table->unsignedInteger('department_id')->nullable();
     $table->foreign('department_id')->references('id')->on('departments');             
});

public function setDepartment($users)
{
        $departmentArray = [];
        $users = DB::table('users')
        ->leftJoin('assignments','assignments.user_id','=','users.id')
        ->leftJoin('departments','assignments.department_id','=','department.id')
            ->select('assignments.id as assignments',
                     'assignments.name as assignment_name',
                     'departments.id as dept_id',
                     'departments.name as dept_name',
                     'users.id as user_id',
                     'users.name as user_name'
                    )
            ->where('user_id','=',$users->id)
            ->get();

        foreach($users as $user) {
                $assignments = $user->assignments;
                $dept_id = $user->dept_id;

                $departmentArray[$user->dept_id] = $user->dept_name;

            }
            $users->department = $departmentArray;
            $users->dept_id = $dept_id;
        }
}

然后每当我尝试这样称呼它时:

public function index() {

    $users = DB::table('users')->get();
    $users = $this->setDepartment($users);
    return view('/users/list', [
        'users' => $users
    ]);

}

我收到了

“试图获取非对象的属性'user_id'”

【问题讨论】:

  • 我想澄清一些部分,你想得到同一部门下的所有用户对吗?
  • 你能检查出是哪一行导致了这个错误吗?
  • @Miggy 说得对。
  • 您的模型中的这些表是否存在关系?如果是这样,你能把它们贴出来吗?

标签: php laravel


【解决方案1】:

'一个部门通过分配有很多用户'

hasManyThrough 的文档:https://laravel.com/docs/5.7/eloquent-relationships#has-many-through

部门模型:

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

'统计每个部门有多少用户'

部门主管:

public function index()
{
    $departments = Department::all();

    foreach($departments as $department)
    {
         // This line returns the number of users for the department
         // Use this however you please
         $department->users->count();
    }
}

可在此处找到 count() 的文档:https://laravel.com/docs/5.7/eloquent#retrieving-aggregates

如果你需要从用户到部门,你可以计算出关系的反面,但我建议你研究一下 eloquent,它对于诸如此类的查询非常方便。如果您需要任何进一步的说明,请告诉我。

编辑:

对于从用户到部门的访问,这是定义关系的一种方式。

用户模型:

// A user belongs to many departments through their assignments
public function departments()
{
    return $this->hasManyThrough('App\Department', 'App\Assignment');
}

这样你就可以像这样访问用户的部门:

$departments = $user->departments();

然后遍历$departments,访问count(),如上所述。

【讨论】:

  • 是的,我确实需要从用户转到部门。我绑定了以下内容: public function user() { return $this->hasManyThrough('App\Assignment', 'App\Department'); } ;然后 $departments = Department::all(); foreach($users as $user) { // 此行返回部门的用户数 // 随意使用 $user->department->count(); } 并且它只返回一个空数组。
  • 从关系这边看用户和部门是什么关系?因为我上面的代码是'一个部门通过分配有很多用户',你是在寻找'一个用户通过分配属于多个部门'吗?
  • 我已经编辑了答案以解释相反的情况,如果需要其他内容,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2019-07-24
  • 2016-02-26
  • 1970-01-01
  • 2014-11-24
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
相关资源
最近更新 更多