我有一个用户模型,该模型具有一个名为"老师"的儿童关系。如何将一个从相关的教师模型添加到返回的数据集的字段?
我希望用户模型返回以具有以下结构:
user.firstname
user.lastname
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('AppRole')->withTimestamps();
}
public function schools()
{
return $this->belongsToMany('AppSchool')->withPivot('suspended', 'rating');
}
public function thisSchool()
{
return $this->belongsToMany('AppSchool')->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('AppEvent')->withPivot('cancelled', 'cancelled_by');
}
public function devices()
{
return $this->hasMany('AppDevice');
}
public function teachingstages()
{
return $this->belongsToMany('AppTeachingstage', 'teacher_teachingstage')->orderBy('teachingstage_id');
}
public function teachingsubjects()
{
return $this->belongsToMany('AppTeachingsubject', 'teacher_teachingsubject');
}
老师。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('AppUser', 'userable');
}
public function criterias()
{
return $this->hasMany('AppCriteria');
}
public function bookingrequests()
{
return $this->belongsToMany('AppBookingrequest')->withPivot('sent', 'sent_at', 'declined', 'created_at', 'updated_at');
}
public function blacklist()
{
return $this->hasMany('AppBlacklist');
}
这是我现有的查询:
$query = User::where('userable_type', 'AppTeacher');
$query->with('userable');
$query->select('firstname', 'lastname')
->addSelect( DB::raw('teachers.address AS address') );
$query->whereDoesntHave('thisSchool');
$otherTeachers = $query->get();
这是一个现有的应用程序,一切都按预期工作;我的问题更多是关于如何添加子模型列作为父模型的别名(然后我将使用新的别名列来计算某些内容(。
预先感谢
k ...
好吧,我自己最终解决了这个问题。
我在用户模型中添加了一个范围:
public function scopeJoinWithTeacher($query)
{
return $query->leftJoin("teachers", "teachers.id", "=", "users.userable_id");
}
然后,我在我的查询中实现了这个新范围,并在get((中从子到父型模型的列列列:
$query = User::where('userable_type', 'AppTeacher');
$query->with('userable');
$query->whereDoesntHave('thisSchool');
$query->JoinWithTeacher();
$query->orderBy( 'distance', 'ASC' );
$otherTeachers = $query->get(['teachers.longitude AS longitude', 'teachers.latitude AS latitude', 'users.*']);
我现在拥有从雄辩的父母模型中的教师孩子关系中的经度和纬度列。在我的具体情况下,我进一步使用这些别名字段来计算距离,并在用户模型中创建一个称为距离的新别名。
希望可以帮助尝试同一件事的人!
k ...