我有一个包含教会、分支和组列的用户表。教会和分支列是整数,而组列是json,因为一个用户可以属于多个组,但只能属于一个教会和分支。我正在使用雄辩来获取用户的详细信息。我只能取教堂和分支,因为只有一个整数在数据库中可用。对于组,接收null。我有这个查询
$member = User::where('id',$id)->with(['church','branch', 'group'])->first()->toArray();
当我dd($member);
时,我收到以下响应
array:13 [▼
"id" => 4
"name" => "Arjun Stanton"
"email" => "jake.schmeler@example.org"
"email_verified_at" => "2021-10-17T07:23:56.000000Z"
"role" => "Tresurer"
"church_id" => 1
"branch_id" => 1
"group_id" => "["7","9"]"
"created_at" => "2021-10-17T07:23:57.000000Z"
"updated_at" => "2021-10-17T07:31:38.000000Z"
"church" => array:5 [▼
"id" => 1
"created_by" => 1
"name" => "St. Joseph"
"created_at" => "2021-10-17T07:25:25.000000Z"
"updated_at" => "2021-10-17T07:25:25.000000Z"
]
"branch" => array:5 [▼
"id" => 1
"name" => "Lee Glenn"
"church_id" => 1
"created_at" => "2021-10-17T07:25:39.000000Z"
"updated_at" => "2021-10-17T07:25:39.000000Z"
]
"group" => null
]
如何在数据库中使用group_id获取记录?
这是错误的关系,你必须使用多对多的这种关系。然后你可以认为结果总是一个包含2个元素的数组。然后,您可以将结果包装在ResourceClass中,并将church作为
'church' => $this->groups[0],
'branch' => $this->groups[1]