因此,我尝试使用以下代码使用Laravel集合返回对象数组:
/**
* View a user's chat rooms.
*
* return \Illuminate\Http\Response|\Laravel\Lumen\Http\ResponseFactory\
*/
public function viewChatRooms()
{
$user = Auth::user(); // @var User $user
$username = $user->username;
$rooms = Room::with('messages')->get()
->filter(function ($val) use ($username){
foreach ($val->users as $user) {
if($user === $username){
return $val;
}
}
});
return response(['rooms' => $rooms]);
}
Run Code Online (Sandbox Code Playgroud)
响应不返回对象数组,而是返回以下内容:
{
"rooms": {
"0": {...},
"3": {...}
}
}
Run Code Online (Sandbox Code Playgroud)
这是期望的结果:
{
"rooms": [
{...},
{...}
]
}
Run Code Online (Sandbox Code Playgroud)
有点受此困扰,有人可以引导我朝正确的方向发展吗?
I am trying to create a relationship in laravel with timestamps. I have this app that allows customers to request a job to a marketplace. When a freelancer on the marketplace finds a job their interested in they propose to complete the job and a new row is queried into the freelancer table.
Here is the code creating the relation:
$marketplace->freelancers()->create([
'request_id' => $id,
'user_id' => $user->id
]);
Run Code Online (Sandbox Code Playgroud)
Here is the Marketplace Model relationship code:
/**
* A request may …Run Code Online (Sandbox Code Playgroud)