计算每个类别有多少用户

时间:2019-01-03 22:37:32

标签: php laravel

我正在尝试获取用户数据,他们获取要排列的部门值,因此我可以将其分组并计算同一部门中有多少用户。

用户表:

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'”

1 个答案:

答案 0 :(得分: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

如果需要从用户转到部门,则可以计算出相反的关系,但是我建议您精打细算,这样的查询可能非常方便。让我知道是否需要进一步澄清。

编辑:

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

用户模型:

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

这样,您可以像这样访问用户的部门:

$departments = $user->departments();

并依次遍历$departments,访问count(),如上所述。