我有两个表:学生表和俱乐部表,它们具有多对多的关系。
作为ajax调用的一部分,我希望返回所有学生及其俱乐部,但我希望排除学生主键,因为这提供了有关系统中学生数量的意外信息。
$students = Student::all()
->with('clubs')
->select('students.name', 'students.grade', 'students.birthday')
->get();这将为clubs返回一个空数组。如果我将第三行改为包含id,它就能完美地工作。
$students = Student::all()
->with('clubs')
->select('students.id', 'students.name', 'students.grade', 'students.birthday')
->get();排除主键的最佳方法是什么?我是不是应该在查询之后通过遍历集合来删除它?
发布于 2020-05-17 12:51:20
我认为你可以使用集合中的forget
https://laravel.com/docs/5.8/collections#method-forget
https://stackoverflow.com/questions/61847060
复制相似问题