Laravel范围查询以计算每个用户的项目类型

时间:2019-02-05 22:50:48

标签: laravel model

我想知道如何在Laravel模型上使用Scopes为我拥有的每个用户计算每种类型的项目。

每个项目都有一个阶段:“赢,输,定价”,并与用户建立关系。

我想知道每个用户按以下类型拥有多少个项目:

用户1:赢2        定价5        输了0

用户2:赢2        定价1        输了3

表格Projects table

项目模型:

protected $table = 'projects';
protected $fillable = ['name', 'phase', 'estimated_date', 'user_id','client_id', 'comments', 'docs', 'approved_docs','contact_id'];

public function user()
{
    return $this->hasOne(User::class, 'id', 'user_id')->withTrashed();
}

**

2 个答案:

答案 0 :(得分:0)

怎么样:

<?php 
User::with(['projects' => function($q){
                return $q->groupBy('projects.phase')
            ->select(\DB::raw("count(*) as count"), 'user_id');

            }])->get();

您想要作用域的id然后在user.php中创建:

<?php 

public function scopePhasecounts($query){
                return $query->with(['projects' => function($q){

                    return $q->groupBy('projects.phase')
                    ->select(\DB::raw("count(*) as count"), 'user_id');
                }])
            }

然后您就可以

User::phasecounts()->get()

答案 1 :(得分:0)

为更好地理解该问题,请显示项目

的数据库表架构

在您提供的信息很少的情况下,类似的事情可能会起作用...

项目模型:

<?php

namespace App;

use App\User;

/**
 * Assumption: You have a table field called **phase** on the project model.
 */
class Project extends Model
{
    /**
     * The relationship: A project belongs to a user.
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Query for WINs.
     */
    public function scopeWin($query)
    {
        return $query->where('phase', 'win);
    }

    /**
     * Query for PRICINGs.
     */
    public function scopePricing($query)
    {
        return $query->where('phase', 'pricing);
    }

    /**
     * Query for LOSSes.
     */
    public function scopeLost($query)
    {
        return $query->where('phase', 'lost);
    }

    /**
     * Query for total number of projects.
     */
    public function totalCounts()
    {
        $wins_count     = $this->win->count();     // call to scopeWin($query)
        $pricings_count = $this->pricing->count(); // call to scopePricing($query)
        $losses_count   = $this->lost->count();    // call to scopeLost($query)

        $total_project_counts = $wins_count + $pricings_count + $losses_count;

        return $total_project_counts;
    }
}

用户模型:

<?php

namespace App;

use App\Project;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The relationship: A user has many projects.
     */
    public function projects()
    {
        return $this->hasMany(Project::class);
    }

    /**
     * Total projects for this user.
     *
     * Using existing relationship instance, 
     * make a call to the appropriate method on the project model.
     */
    public function totalProjectCounts()
    { 
        return $this->projects()->totalCounts();
    }

    // User projects with phase Win.
    public function projectWinCounts()
    { 
        return $this->projects()->win()->count();
    }

    // User projects with phase Pricing.
    public function projectPricingCounts()
    { 
        return $this->projects()->pricing()->count();
    }

    // User projects with phase Lost.
    public function projectLostCounts()
    { 
        return $this->projects()->lost()->count();
    }
}

对于给定的用户,您可以像这样检查单个总数:

$user->totalProjectCounts(); 

auth()->user()->totalProjectCounts(); 

Auth::user()->totalProjectCounts(); 

希望这会有所帮助。否则,请提供有关您面临的问题的更多信息。

<table>
    <tr>
        <th> USER </th>
        <th> WIN </th>
        <th> PRICING </th>
        <th> LOST </th>
        <th> TOTAL </th>
    </tr>

    @foreach ($users as $user)
    <tr>
        <td>{{ $user->name }}</td>
        <td>{{ $user->projectWinCounts() }}</td>
        <td>{{ $user->projectPricingCounts() }}</td>
        <td>{{ $user->projectLostCounts() }}</td>
        <td>{{ $user->totalProjectCounts() }}</td>
    </tr>
    @endforeach
</table>

我认为 belongsTo(User :: class) hasOne(user :: class)更合适。