如何在Laravel中按员工获取任务计数。

huangapple go评论64阅读模式
英文:

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 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))

员工/用户迁移

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(&#39;tasks&#39;)
    -&gt;select(DB::raw(&quot;MONTHNAME(tasks.created_at) as month_name&quot;))
    -&gt;join(&#39;users&#39;, &#39;tasks.assigned_to&#39;, &#39;=&#39;, &#39;users.id&#39;)
    -&gt;where([&#39;users.role_id&#39;, &#39;=&#39;, auth()-&gt;user()-&gt;role_id])
    -&gt;whereYear(&#39;tasks.created_at&#39;, date(&#39;Y&#39;))
    -&gt;groupBy(DB::raw(&quot;MONTHNAME(tasks.created_at)&quot;))
    -&gt;get();

 
$labels = $user_tasks-&gt;keys();
$data = $user_tasks-&gt;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(&#39;users&#39;, function (Blueprint $table) {
        $table-&gt;id();
        $table-&gt;string(&#39;name&#39;);
        $table-&gt;string(&#39;username&#39;);
        $table-&gt;string(&#39;email&#39;)-&gt;nullable()-&gt;unique();
		$table-&gt;date(&#39;date_of_joining&#39;)-&gt;nullable();
        $table-&gt;date(&#39;dob&#39;)-&gt;nullable();
        $table-&gt;string(&#39;mobile&#39;)-&gt;nullable();
        $table-&gt;timestamp(&#39;email_verified_at&#39;)-&gt;nullable();
        $table-&gt;string(&#39;password&#39;);
        $table-&gt;integer(&#39;role_id&#39;)-&gt;nullable();
        $table-&gt;rememberToken();
        $table-&gt;softDeletes();
        $table-&gt;timestamps();
		$table-&gt;integer(&#39;status&#39;)-&gt;default(0);
    });
}

Tasks migration

public function up()
{
    Schema::create(&#39;tasks&#39;, function (Blueprint $table) {
        $table-&gt;id();
        // $table-&gt;integer(&#39;task_id&#39;);
        $table-&gt;string(&#39;title&#39;);
        $table-&gt;text(&#39;description&#39;)-&gt;nullable();
        $table-&gt;timestamp(&#39;deadline_date&#39;)-&gt;nullable();
        $table-&gt;integer(&#39;assigned_by&#39;);
        $table-&gt;integer(&#39;assigned_to&#39;);
        $table-&gt;integer(&#39;follow_up&#39;)-&gt;nullable();
        $table-&gt;integer(&#39;priority&#39;)-&gt;default(1);
        $table-&gt;integer(&#39;work_status&#39;)-&gt;default(1);
        $table-&gt;integer(&#39;status&#39;)-&gt;default(0);
        $table-&gt;string(&#39;remarks&#39;)-&gt;nullable();
        $table-&gt;integer(&#39;updated_by&#39;)-&gt;nullable();
        $table-&gt;timestamp(&#39;status_updated_date&#39;)-&gt;nullable();
        $table-&gt;index(&#39;id&#39;);
        $table-&gt;timestamps();
    });
}

In this assigned_to column saves the user_id (id in user table). I want to get the result like this

lables varibale = [&#39;EmpName 1&#39;, &#39;EmpName 2&#39;, &#39;EmpName 3&#39;, &#39;EmpName 4&#39;];
count_of_each_user_task = [&#39;5&#39;, &#39;8&#39;, &#39;2&#39;, &#39;6&#39;]

答案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(&#39;tasks&#39;)
    -&gt;selectRaw(&quot;CONCAT(users.name, &#39; (EmpName &#39;, users.id, &#39;)&#39;) as employee_name, COUNT(*) as task_count&quot;)
    -&gt;join(&#39;users&#39;, &#39;tasks.assigned_to&#39;, &#39;=&#39;, &#39;users.id&#39;)
    -&gt;where(&#39;users.role_id&#39;, &#39;=&#39;, auth()-&gt;user()-&gt;role_id)
    -&gt;whereYear(&#39;tasks.created_at&#39;, date(&#39;Y&#39;))
    -&gt;groupBy(&#39;users.id&#39;, &#39;users.name&#39;)
    -&gt;get();

$labels = $user_tasks-&gt;pluck(&#39;employee_name&#39;)-&gt;toArray();
$count_of_each_user_task = $user_tasks-&gt;pluck(&#39;task_count&#39;)-&gt;toArray();

You have a couple of issues with your query try this one. Let me one if it will not work

huangapple
  • 本文由 发表于 2023年7月31日 19:52:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定