【问题标题】:Laravel Scope querying to count project type for each userLaravel Scope 查询以计算每个用户的项目类型
【发布时间】:2019-02-22 14:22:49
【问题描述】:

我想知道如何在 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


    【解决方案1】:

    怎么样:

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

    【讨论】:

      【解决方案2】:

      为了更好地理解问题,请显示 project 的 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) 更合适。

      【讨论】:

      • 我编辑问题并添加更多信息。我想在表格上显示每个用户拥有的每个阶段的项目数量
      猜你喜欢
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 2011-10-16
      • 1970-01-01
      相关资源
      最近更新 更多