英文:
How to get Employee wise tasks count in laravel along
问题
我想要获取每个员工在一个月内的任务数量并集成到柱状图中,我已经成功获取了基于一个月的任务总数,但不清楚如何基于用户来做,假设我有10个员工,我需要每个用户有多少任务并显示在柱状图中,包括任务数量和员工的姓名。
我做了类似于这样的操作,但不确定逻辑是否正确,我不知道为什么我这样做,但希望能得到帮助。
$user_tasks = DB::table('tasks')
->select(DB::raw("MONTHNAME(tasks.created_at) as month_name"))
->join('users', 'tasks.assigned_to', '=', 'users.id')
->where(['users.role_id', '=', auth()->user()->role_id])
->whereYear('tasks.created_at', date('Y'))
->groupBy(DB::raw("MONTHNAME(tasks.created_at)"))
->get();
$labels = $user_tasks->keys();
$data = $user_tasks->values();
报错信息
Illuminate\Database\QueryException
SQLSTATE[42S22]: 无法找到列: 1054 未知列 '0' 在 'where clause' 中 (SQL: select MONTHNAME(tasks.created_at) as month_name fromtasks
inner joinusers
ontasks
.assigned_to
=users
.id
where (0
= users.role_id and1
= = and2
= 1) and year(tasks
.created_at
) = 2023 group by MONTHNAME(tasks.created_at))
员工/用户迁移
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username');
$table->string('email')->nullable()->unique();
$table->date('date_of_joining')->nullable();
$table->date('dob')->nullable();
$table->string('mobile')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->integer('role_id')->nullable();
$table->rememberToken();
$table->softDeletes();
$table->timestamps();
$table->integer('status')->default(0);
});
}
任务迁移
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
// $table->integer('task_id');
$table->string('title');
$table->text('description')->nullable();
$table->timestamp('deadline_date')->nullable();
$table->integer('assigned_by');
$table->integer('assigned_to');
$table->integer('follow_up')->nullable();
$table->integer('priority')->default(1);
$table->integer('work_status')->default(1);
$table->integer('status')->default(0);
$table->string('remarks')->nullable();
$table->integer('updated_by')->nullable();
$table->timestamp('status_updated_date')->nullable();
$table->index('id');
$table->timestamps();
});
}
在assigned_to
列中保存了user_id
(在user
表中的id
)。我想要的结果如下:
$labels = ['EmpName 1', 'EmpName 2', 'EmpName 3', 'EmpName 4'];
$count_of_each_user_task = [5, 8, 2, 6];
英文:
I want to get each employees task count in a month and integrate into bar chart,i managed to get total count of tasks based in a month i am confused how to do that user based,let's say i have 10 employees,i need how many tasks each user have and show into a barchart along with task count and name of the employees
i did kind of like this i am not sure the logic is correct or not,i have no idea why i did this.but help would be appreciated
<!-- language-all: php -->
$user_tasks = DB::table('tasks')
->select(DB::raw("MONTHNAME(tasks.created_at) as month_name"))
->join('users', 'tasks.assigned_to', '=', 'users.id')
->where(['users.role_id', '=', auth()->user()->role_id])
->whereYear('tasks.created_at', date('Y'))
->groupBy(DB::raw("MONTHNAME(tasks.created_at)"))
->get();
$labels = $user_tasks->keys();
$data = $user_tasks->values();
Error thrown
> Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select MONTHNAME(tasks.created_at) as month_name from tasks
inner join users
on tasks
.assigned_to
= users
.id
where (0
= users.role_id and 1
= = and 2
= 1) and year(tasks
.created_at
) = 2023 group by MONTHNAME(tasks.created_at))
Employee/User Migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username');
$table->string('email')->nullable()->unique();
$table->date('date_of_joining')->nullable();
$table->date('dob')->nullable();
$table->string('mobile')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->integer('role_id')->nullable();
$table->rememberToken();
$table->softDeletes();
$table->timestamps();
$table->integer('status')->default(0);
});
}
Tasks migration
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
// $table->integer('task_id');
$table->string('title');
$table->text('description')->nullable();
$table->timestamp('deadline_date')->nullable();
$table->integer('assigned_by');
$table->integer('assigned_to');
$table->integer('follow_up')->nullable();
$table->integer('priority')->default(1);
$table->integer('work_status')->default(1);
$table->integer('status')->default(0);
$table->string('remarks')->nullable();
$table->integer('updated_by')->nullable();
$table->timestamp('status_updated_date')->nullable();
$table->index('id');
$table->timestamps();
});
}
In this assigned_to
column saves the user_id
(id
in user
table). I want to get the result like this
lables varibale = ['EmpName 1', 'EmpName 2', 'EmpName 3', 'EmpName 4'];
count_of_each_user_task = ['5', '8', '2', '6']
答案1
得分: 1
$user_tasks = DB::table('tasks')
->selectRaw("CONCAT(users.name, ' (EmpName ', users.id, ')') as employee_name, COUNT(*) as task_count")
->join('users', 'tasks.assigned_to', '=', 'users.id')
->where('users.role_id', '=', auth()->user()->role_id)
->whereYear('tasks.created_at', date('Y'))
->groupBy('users.id', 'users.name')
->get();
$labels = $user_tasks->pluck('employee_name')->toArray();
$count_of_each_user_task = $user_tasks->pluck('task_count')->toArray();
英文:
$user_tasks = DB::table('tasks')
->selectRaw("CONCAT(users.name, ' (EmpName ', users.id, ')') as employee_name, COUNT(*) as task_count")
->join('users', 'tasks.assigned_to', '=', 'users.id')
->where('users.role_id', '=', auth()->user()->role_id)
->whereYear('tasks.created_at', date('Y'))
->groupBy('users.id', 'users.name')
->get();
$labels = $user_tasks->pluck('employee_name')->toArray();
$count_of_each_user_task = $user_tasks->pluck('task_count')->toArray();
You have a couple of issues with your query try this one. Let me one if it will not work
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论