Laravel 创建具有多个一对多关系值的数据列表
我有一个表结构,其中包含从下拉列表中选择的id列表。 让我们用以下格式来表示:Laravel 创建具有多个一对多关系值的数据列表,laravel,laravel-5.5,Laravel,Laravel 5.5,我有一个表结构,其中包含从下拉列表中选择的id列表。 让我们用以下格式来表示: { "id":8, "program":"Master of Computer", //with program_id from program table "course":"software engineering" //with course_id from course table }, { "id":7, "program":"Bachelor of Pharmacy
{
"id":8,
"program":"Master of Computer", //with program_id from program table
"course":"software engineering" //with course_id from course table
},
{
"id":7,
"program":"Bachelor of Pharmacy", //with program_id from program table
"course":"Structure Programing" //with course_id from course table
}
id、教师id、批次id、课程id、计划id、部门id
这些字段通过ajax调用保存到数据库中。现在我想获取与此id关联的名称
我有这样一个函数:
public function store(StoreEmployeesRequest $request)
{
$teacherCourse = TeacherCourse::create($request->all());
}
在这里,创建TeacherCourse之后,我想从TeacherCourse表中获取与$TeacherCourse->teacher\u id关联的所有值
合并该值并按以下格式创建数组:
{
"id":8,
"program":"Master of Computer", //with program_id from program table
"course":"software engineering" //with course_id from course table
},
{
"id":7,
"program":"Bachelor of Pharmacy", //with program_id from program table
"course":"Structure Programing" //with course_id from course table
}
教师课程中的关系
我试图使用此操作获取值,但失败。如何通过从多个表中获取值来创建单个数组
$teacherCourse = $teachercourse->program->map(function ($program)
{
return $program->name;
});
试试这个:
$teacherCourse = $teachercourse->map(function ($teacherCourse)
{
return [
'id' => $teacherCourse->id,
'program' => $teacherCourse->program()->pluck('name'),
'course' => $teacherCourse->course()->pluck('name');
];
})->toArray();
我希望能对您有所帮助让我试试解决方案,然后再联系您!我得到了一个嵌套数组,我只想在'program'上绑定一个字符串名和课程,并将courseedit代码绑定到program->first->name或program->name@AkshayKritii我认为应该将列表更改为pluck@AkshayKriti如果只想选择列是,则应使用Pull,