Laravel Scope查询为每个用户计算项目类型

问题描述 投票:0回答:2

我想知道如何在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();
}

**

laravel model
2个回答
0
投票

怎么样 :

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

            }])->get();

如果你想要范围,那么在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()

0
投票

为了更好地理解该问题,请显示项目的db表模式

有了你提供的小信息,这样的东西可以工作......

项目模型:

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

© www.soinside.com 2019 - 2024. All rights reserved.