现在,我雄辩地尝试此查询:
'MentorId' => $employee->intern(true)->mentor(true)->MentorId,
在我employee
和intern
模型中,我得到了这个:
实习生
/**
* @return mixed
*/
public function intern($withTrashed = false)
{
if($withTrashed == true)
{
return $this->belongsTo(internModel::class, 'InternId')->withTrashed();
}
return $this->belongsTo(internModel::class,'InternId');
}
导师
/**
* @return mixed
*/
public function mentor($withTrashed = false)
{
if($withTrashed == true)
{
return $this->belongsTo(mentorModel::class, 'MentorId')->withTrashed();
}
return $this->belongsTo(mentorModel::class,'MentorId');
}
但它崩溃了:
BadMethodCallException in Builder.php line 2148:
Call to undefined method Illuminate\Database\Query\Builder::mentor()
我该如何解决?
- 编辑 -
员工
<?php
namespace App\src\employee;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\src\department\Department as departmentModel;
use App\src\employee\Employee as employeeModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\src\intern\Intern as internModel;
use App\src\mentor\Mentor as mentorModel;
use App\src\employee\Role as roleModel;
class Employee extends Authenticatable
{
use SoftDeletes;
use EmployeeServiceTrait;
/**
* table name
*/
protected $table = 'employee';
/**
* Mass assignment fields
*/
protected $fillable = ['RoleId', 'DepartmentId', 'InternId', 'FirstName', 'LastName', 'Bio','api_token', 'email', 'LinkedIn', 'password', 'Address', 'Zip', 'City', 'ProfilePicture', 'BirthDate', 'StartDate', 'EndDate', 'Suspended','LinkedIn'];
/**
* Primarykey
*/
protected $primaryKey = 'EmployeeId';
/**
* Deleted_at
*/
protected $dates = ['deleted_at'];
/**
* @return mixed
*/
public function role()
{
return $this->belongsTo(roleModel::class,'RoleId');
}
/**
* @return mixed
*/
public function intern($withTrashed = false)
{
if($withTrashed == true)
{
return $this->belongsTo(internModel::class, 'InternId')->withTrashed();
}
return $this->belongsTo(internModel::class,'InternId');
}
/**
* @return mixed
*/
public function department()
{
return $this->belongsTo(departmentModel::class,'DepartmentId');
}
/**
* @return mixed
*/
public function mentor()
{
return $this->belongsTo(mentorModel::class,'MentorId');
}
/**
* @return mixed
*/
public function employees()
{
return $this->hasManyThrough(employeeModel::class,departmentModel::class,'CompanyId','DepartmentId');
}
/**
* @param $role
* @return bool
*/
public function hasRole($role)
{
if(strtolower($this->role->RoleName) == strtolower($role))
{
return true;
}
return false;
}
}
您遇到的问题是,任何雄辩的关系对象实际上都是的实例Relation
。这意味着当您创建关系时,您实际上会返回一个集合(的实例为Builder
);提示您的错误:
Builder.php第2148行中的BadMethodCallException:调用未定义的方法Illuminate \ Database \ Query \ Builder :: mentor()
在不对您的代码进行任何修改的情况下,简单的解决方案将是:
'MentorId' => $employee->intern(true)->first()->mentor(true)->first()->MentorId;
但是,您可以使用如下所示的重载:
'MentorId' => $employee->intern->mentor->MentorId;
虽然这将不包括您的withTrashed
。但是,您可以将您的关系调整为类似以下内容:
public function intern($withTrashed = false)
{
$relation = $this->belongsTo(internModel::class, 'InternId');
if($withTrashed == true)
{
return $relation->withTrashed()->first();
}
return $relation->first();
}
但是我不建议这样做,因为以后如果您尝试使用类似WhereHas
错误的东西,那将是错误的。也就是说,另一种方式是按照以下方式进行操作:
public function intern()
{
return $this->belongsTo(internModel::class, 'InternId');
}
然后像这样被扔垃圾:
'MentorId' => $employee->intern()->withTrashed()->first()->mentor()->withTrashed()->first()->MentorId;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句