【发布时间】:2017-07-13 22:45:24
【问题描述】:
我有这个 laravel 应用程序 [实际上是 iPhone 应用程序的 API],我们有可以关注这个“类”或组的用户,并且在类中人们可以发布很多帖子,我的问题是如何进行查询对您拥有的所有课程的所有新帖子进行提要。
这是我的桌子:
用户
id - 姓名 - 电子邮件 - 密码
类
id - creators_id - 名称 - 描述
Classes_Users
user_id - clase_id
帖子
id - creators_id - clase_id - 标题 - 文本
型号:
class User extends Authenticatable
{
use Notifiable;
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
protected $primaryKey = 'id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password','about'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function posts(){
return $this->hasMany('App\Posts');
}
public function clases(){
return $this->belongsToMany('App\Clase','clase_user', 'user_id');
}
public function claseAdmin(){
return $this->hasMany('App\Clase');
}
}
class Clase extends Model
{
protected $fillable = [
'users_id', 'descripcion', 'name', 'tema_id',
];
public function Tema(){
return $this->hasOne('App\Temas');
}
public function User(){
return $this->hasOne('App\User');
}
public function posts(){
return $this->hasMany('App\Posts');
}
}
class Posts extends Model
{
protected $fillable = [
'users_id', 'clase_id', 'text', 'archivo',
];
public function Clase(){
return $this->hasOne('App\Clase');
}
}
【问题讨论】: