检索具有 _____________ 关系的记录?

Retrieving records with _____________ relationship?

我有 5 个table。

用户表=>这个table存储用户的数据。
考试 table => 这 table 存储考试数据。
Feeds table => 这 table 存储 feeds 的数据。
Exam_User_Pivot table => 此 table 存储用户和考试的数据意味着 user_id 和 exam_id.
Exam_Feed_Pivot table => 此 table 存储检查和提要的数据意味着 exam_id 和 feed_id.

现在,我想找回号码。每个供稿的用户数量?

我有三个模型:

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();
    }
}
  • Users have many-to-many relationship with Exams
  • Feeds have many-to-many relationship with Exams

我要找回号码。每个提要的用户数量我不知道可以使用哪种关系?

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

添加到考试模型:

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

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

我想找回号码。每个供稿的用户数 .....

推理基于以下三点:

  1. 获取提要,
  2. 然后获取该提要的考试(一个提要有很多考试)
  3. 然后获取每个考试的用户(这里我们将使用一个循环来确保我们从上面的 2 个开始计算每个考试的用户)

循环遍历所有提要:

 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 个用户

供稿 otherfeed 有 38 个用户

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

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";
    }
});

然后打开浏览器并输入 link:

http:///YOUR_PROJECT_NAME_HERE/getusers 然后按回车键:

注意:将 YOUR_PROJECT_NAME_HERE 替换为您的实际项目名称。确保您的数据库中已有一些检查、用户和提要。 我的结果

/**
 * 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();
}