我们有以下使用$ with的类:
class CargaHorasEmpleado extends Model
{
protected $table = "empleados_horas";
protected $with = ["tipoTarea", "proyecto", "empleado", "empleadoQueHizoLaCarga"];
public function tipoTarea()
{
return $this->belongsTo('App\TipoTarea', 'id_tipo_tarea', 'id')->withTrashed();
}
public function empleado()
{
return $this->belongsTo('App\Empleado', 'id_empleado', 'id')->withTrashed();
}
public function empleadoQueHizoLaCarga()
{
return $this->belongsTo('App\Empleado', 'id_empleado_cargo_hs', 'id')->withTrashed();
}
public function proyecto()
{
return $this->belongsTo('App\Proyecto', 'id_proyecto', 'id')->withTrashed();
}
}
这是TipoTarea类
namespace App;
use Illuminate\Database\Eloquent\Model;
class TipoTarea extends Model
{
protected $table = 'tipos_tareas';
public $timestamps = false;
protected $fillable = [
'titulo', 'descripcion'
];
}
Thep页面引发错误:“调用模型[App \ CargaHorasEmpleado]上的未定义关系[tipoTarea]”。那是唯一不起作用的关系。其他都很好。怎么了?
答案 0 :(得分:-1)
嗯,这种关系不是叫做“ tipoTarea”吗?您写了“ tiposTarea”
答案 1 :(得分:-1)
问题是我的课程“ TipoTarea”没有使用软删除。因此,错误在于使用“ WithTrashed”方法。正确的方法是:
public function tipoTarea()
{
return $this->belongsTo('App\TipoTarea', 'id_tipo_tarea', 'id');
}