提问者:小点点

正在检索具有uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu关系的记录?


我有五张桌子。

用户标签=

现在,我想检索每个提要的用户数?

我有三种型号:

1.用户模型

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    protected $primaryKey = 'uid';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'phone',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function exams()
    {
        return $this->belongsToMany(exam::class, 'exam_user', 'user_id', 'exam_id')->withTimeStamps();
    }
}

2.考试模式

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class exam extends Model
{
    protected $primaryKey = 'eid';

    protected $fillable = [
        'exam_name',
    ];
}

3.饲料模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Feed extends Model
{
    protected $primaryKey = 'fid';

    protected $fillable = [
        'feed_type', 'title', 'description', 'feed_media', 'source',
    ];

    public function feedexams()
    {
        return $this->belongsToMany(exam::class, 'exam_feed', 'feed_id', 'exam_id')->withTimeStamps();
    }
}
  • 用户与考试有多对多的关系
  • 提要与考试有多对多的关系

我想检索每个提要的用户数,我不知道可以使用哪种关系?


共2个答案

匿名用户

因此,首先将以下两个函数添加到您的考试模型中。

添加到考试模型:

 public function feeds() {
         return $this->belongsToMany('App\Feed')->withTimeStamps(); // 
  }

 public function users() {
    return $this->belongsToMany('App\User')->withTimeStamps(); //
  }

我想检索每个提要的用户数......

推理基于以下三点:

  1. 获取提要,

将所有提要循环为:

 foreach(App\Feed::all() as $feed) { 
 //here you are accessing all related exams using the function feedexams() that you have created in the feeds model
 
   $exams  = $feed->feedexams();  

 //because each exam will have its own users, we need a way to count the users for each exam, and finally ensure we sum or count all of them. we create a counter called $user_per_feed_count to keep or remember the users count as we loop through all exams. 
 //Finally we will output this counter which will have the total of all users belonging to exams which belong to the feed.

   $users_count = 0; //initialize a users counter with zero

   foreach($exams as $exam) {
    //now we update our counter by adding the number of users for each exam
    $users_count +=  $exam->users()->count();
   }
   //HERE IS YOUR no. of users per each feed.
  echo "Feed " . $feed->name . "has " . $users_count . " users"; 
 }

预期产出如下...

Feed myfeed有10个用户

Feed-otherfeed有38个用户

现在要测试它,只需转到您的routes/web。并添加以下代码

Route::get('getusers',  function () {
    foreach (App\Feed::all() as $feed) {
        $exams  = $feed->feedexams();
        $users_count = 0; //initialize a users counter with zero
        foreach ($exams as $exam) {
        //$users_count +=  $exam->users()->count();
        $users_count +=  $exam->has('users') ? $exam->users()->count() : 0;

    }
        echo "Feed " . $feed->name . "has " . $users_count . " users";
    }
});

然后打开浏览器并键入以下链接:

http://YOUR_PROJECT_NAME_HERE/getusers然后按enter键:

注意:用实际项目名称替换此处的项目名称。确保数据库中已有一些考试、用户和提要。我的结果

匿名用户

/**
 * Get all of the user for the feed. define in Feed Model
 */
public function feedUsers()
{
    return $this->hasManyThrough(App\Models\Exam::class App\Models\User::class);
}
$feeds = App\Feed::all();
foreach($feeds as $feed){
    echo $feed->feedUsers->count();
}