从 laravel/lumen 中的两个雄辩的关系模型获取数据的正确方法是什么?
我正在尝试与一些雄辩的表建立关系,并尝试制作实际上从两个关系表中检索数据的 api。这里一张照片就明白了。
让我们考虑员工和员工类型来简化问题。我的迁移代码看起来像这样
Schema::create('os_employee_types', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('priority')->default(0);
$table->tinyInteger('status')->default('1');
$table->timestamps();
});
,并且,
Schema::create('os_employees', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->bigInteger('employee_type')->unsigned()->nullable();
$table->timestamps();
$table->foreign('employee_type')->references('id')->on('os_employee_types');
});
在员工表中,employee_type 是员工类型表中的外键,与员工类型表上的 id 相关。我的模型看起来像
class OsEmployeeType extends Model
{
public function OsEmployee()
{
return $this->hasOne(OsEmployee::class);
}
}
现在
class OsEmployee extends Model
{
public function OsEmployeeType()
{
return $this->belongsTo(OsEmployeeType::class);
}
}
控制器看起来像
public function check(){
$employee = OsEmployee::with('OsEmployeeType')->get();
return new OsEmployeeMaxCollection($employee);
}
我得到了响应
{
"data": [
{
"id": 56,
"name": "Arafat Rahman",
"employee_type": 2,
"created_at": "2022-03-01T11:23:05.000000Z",
"updated_at": "2022-03-01T11:23:05.000000Z",
}
]
}
这是来自 os_employee 表的绝对数据。但我想要具有员工类型的数据。我想要这样的响应:
{
"data": [
{
"id": 56,
"name": "Arafat Rahman",
"employee_type": {
"id": 2,
"name": "employee_type 1",
"priority": 10,
"status": 1
},
"created_at": "2022-03-01T11:23:05.000000Z",
"updated_at": "2022-03-01T11:23:05.000000Z",
}
]
}
Here is the dd($employee)
OsEmployeeMaxCollection 看起来像
class OsEmployeeMaxCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
我的错误是什么?我怎样才能得到像我的描述这样的数据。
I am trying to build up a relation with some eloquent table and try to make api that actually retrieve data from two relational table. Here a photo to understand.
Lets consider employees and employee_types for simplify the problem. My migration code looks like
Schema::create('os_employee_types', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('priority')->default(0);
$table->tinyInteger('status')->default('1');
$table->timestamps();
});
And,
Schema::create('os_employees', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->bigInteger('employee_type')->unsigned()->nullable();
$table->timestamps();
$table->foreign('employee_type')->references('id')->on('os_employee_types');
});
Here in employee table, employee_type is a foreign key in employee types table that relation with id on employee type table. And my model looks like
class OsEmployeeType extends Model
{
public function OsEmployee()
{
return $this->hasOne(OsEmployee::class);
}
}
And,
class OsEmployee extends Model
{
public function OsEmployeeType()
{
return $this->belongsTo(OsEmployeeType::class);
}
}
Now the controller looks like
public function check(){
$employee = OsEmployee::with('OsEmployeeType')->get();
return new OsEmployeeMaxCollection($employee);
}
I got the response
{
"data": [
{
"id": 56,
"name": "Arafat Rahman",
"employee_type": 2,
"created_at": "2022-03-01T11:23:05.000000Z",
"updated_at": "2022-03-01T11:23:05.000000Z",
}
]
}
This is absolute data from os_employee table. but I want the data with employee type. I want the response like
{
"data": [
{
"id": 56,
"name": "Arafat Rahman",
"employee_type": {
"id": 2,
"name": "employee_type 1",
"priority": 10,
"status": 1
},
"created_at": "2022-03-01T11:23:05.000000Z",
"updated_at": "2022-03-01T11:23:05.000000Z",
}
]
}
Here is the dd($employee)
And OsEmployeeMaxCollection looks like
class OsEmployeeMaxCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
What was my mistake or wrong? How can I get data like my description.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效。如果没有 OsEmployeeType,则此字段不应为空。
如果您想要 json 响应
并将代码迁移更改为:
将模型更改为:
This should work. If it does not have OsEmployeeType this field should not be empty.
If you want json reponse
and change your code migration to:
And your model to: