【发布时间】:2018-09-13 19:54:39
【问题描述】:
我有一个用户模型,它有一个名为教师的子关系。如何将相关教师模型中的字段添加到父用户模型返回的数据集中?
我希望返回的用户模型具有如下结构:
用户名
用户名
user.teacher.address
我尝试使用以下及其变体但没有成功:
$query->select('firstname', 'lastname')
->addSelect( \DB::raw('teachers.address AS address') );
user.php 模型:
use Notifiable;
use SoftDeletes;
use EncryptableTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'firstname',
'firstname_h',
'lastname',
'lastname_h',
'email',
'email_h',
'password',
'userable_id',
'userable_type'
];
/**
* The attributes that are mass encryptable, using the EncryptableTrait.
*
* @var array
*/
protected $encryptable = [
'firstname',
'lastname',
'email',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function userable()
{
return $this->morphTo();
}
public function teacher() // Using custom BelongsToMorph relationship type
{
return BelongsToMorph::build($this, Teacher::class, 'userable');
}
public function roles()
{
return $this->belongsToMany('App\Role')->withTimestamps();
}
public function schools()
{
return $this->belongsToMany('App\School')->withPivot('suspended', 'rating');
}
public function thisSchool()
{
return $this->belongsToMany('App\School')->where('school_id', Auth::user()->userable->id)->withPivot('id', 'suspended', 'rating', 'notes');
}
public function addRole($user, $role_id)
{
$user->roles()->attach($role_id);
}
public function removeRole($user, $role_id)
{
$user->roles()->detach($role_id);
}
public function isAdmin($user)
{
foreach ($user->roles as $role) {
if($role->id == 4) {
return true;
} else {
$return = false;
}
}
return $return;
}
public function isSchoolAdmin($user)
{
foreach ($user->roles as $role) {
if($role->id == 2) {
return true;
} else {
$return = false;
}
}
return $return;
}
public function events()
{
return $this->belongsToMany('App\Event')->withPivot('cancelled', 'cancelled_by');
}
public function devices()
{
return $this->hasMany('App\Device');
}
public function teachingstages()
{
return $this->belongsToMany('App\Teachingstage', 'teacher_teachingstage')->orderBy('teachingstage_id');
}
public function teachingsubjects()
{
return $this->belongsToMany('App\Teachingsubject', 'teacher_teachingsubject');
}
teacher.php 模型:
use EncryptableTrait;
protected $table = "teachers";
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'mobile',
'dob',
'gender',
'address',
'postcode',
'latitude',
'longitude',
'max_distance',
'public',
'photo',
'experience',
'active',
'verified',
'payscale',
'ta_number',
'temp_or_perm',
'locked'
];
/**
* The attributes that are mass encryptable, using the EncryptableTrait.
*
* @var array
*/
protected $encryptable = [
'mobile',
'address',
'postcode',
'experience',
'ta_number'
];
public function user()
{
return $this->morphOne('App\User', 'userable');
}
public function criterias()
{
return $this->hasMany('App\Criteria');
}
public function bookingrequests()
{
return $this->belongsToMany('App\Bookingrequest')->withPivot('sent', 'sent_at', 'declined', 'created_at', 'updated_at');
}
public function blacklist()
{
return $this->hasMany('App\Blacklist');
}
这是我有问题的现有查询:
$query = User::where('userable_type', 'App\Teacher');
$query->with('userable');
$query->select('firstname', 'lastname')
->addSelect( \DB::raw('teachers.address AS address') );
$query->whereDoesntHave('thisSchool');
$otherTeachers = $query->get();
这是一个现有的应用程序,一切都按预期工作;我的问题更多是关于如何将子模型列作为别名添加到父模型(然后我将使用新的别名列进行计算)。
提前致谢,
K...
【问题讨论】:
-
老师的“孩子”关系是什么意思?老师也是用户吗?或者它是一个不同的表?请写下你的数据库结构:)
-
分享你的完整代码?
-
教师模型与用户模型具有多态关系,以扩展特定的用户类型。
-
@MatthiasS 按要求更新了我的帖子
-
@C2486 按要求更新了我的帖子
标签: php sql laravel-5 eloquent laravel-eloquent