I have 5 tables.
- User tabel => This table stores the data of users.
- Exam table => This table stores the data of exams.
- Feeds table => This table stores the data of feeds.
- Exam_User_Pivot table => This table stores the data of users and exams means the user_id and exam_id.
- Exam_Feed_Pivot table => This table stores the data of exams and feeds means the exam_id and feed_id.
Now, I want to retrieve the no. of users per each feed ?
I have three models :
1) User model
<?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) Exam model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class exam extends Model
{
protected $primaryKey = 'eid';
protected $fillable = [
'exam_name',
];
}
3) Feed model
<?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.
I want to retrieve the no. of users per each feed And I don't know which relationship can be use ?