使用Laravel 9,在我的应用程序中,头衔和工资之间存在一对多与员工模型的关系. 员工模型
public function titles(): HasMany
{
return $this->hasMany(Title::class, 'emp_id');
}
public function salaries(): HasMany
{
return $this->hasMany(Salary::class, 'emp_id');
}
薪酬模式
public function employee(): BelongsTo
{
return $this->belongsTo(Employee::class, 'emp_no');
}
职称模型
public function employee(): BelongsTo
{
return $this->belongsTo(Employee::class, 'emp_no');
}
现在我将使用EmployeeController getEmployee函数以及EmployeeController.php来获取所有数据
use Illuminate\Http\Request;
use App\Models\Employee;
use App\Models\Title;
use App\Models\Salary;
class EmployeeController extends Controller
{
public function getEmployee() {
return response()->json(Employee::with('title','salary')->get(), 200);
}
}
我的api.php调用如下所示
Route::get('employees','App\Http\Controllers\EmployeeController@getEmployee');
但在错误消息之后也会显示结果
Illuminate\Database\Eloquent\RelationNotFoundException: Call to undefined relationship [title] on model [App\Models\Employee]. in file
个
那我怎么才能解决这个问题呢?