如何在父数组中分组相同的值?

时间:2019-02-15 13:53:32

标签: php laravel

我想为我的ios应用创建故事,我正在尝试获取有用的json响应。我想在该用户数组的一个组中显示相同的用户故事。

当前JSON结果;

{
"current_page": 1,
"data": [
{
"id": 3,
"name": "Muhammed Ali Yüce",
"username": "ali",
"avatar": "1544128196.png",
"stories": {
"id": 3,
"user_id": 3,
"image": "1550228567.jpg",
"created_at": "2019-02-15 11:02:47",
"updated_at": "2019-02-15 11:02:47"
}
},
{
"id": 2,
"name": "Ömer Faruk YÜCE",
"username": "omer",
"avatar": "1544128227.png",
"stories": {
"id": 2,
"user_id": 2,
"image": "1550228407.jpg",
"created_at": "2019-02-15 11:00:08",
"updated_at": "2019-02-15 11:00:08"
}
},
{
"id": 2,
"name": "Ömer Faruk YÜCE",
"username": "omer",
"avatar": "1544128227.png",
"stories": {
"id": 1,
"user_id": 2,
"image": "1550072626.jpg",
"created_at": "2019-02-13 15:43:47",
"updated_at": "2019-02-13 15:43:47"
}
}
],
"first_page_url": "/?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "/?page=1",
"next_page_url": null,
"path": "/",
"per_page": 10,
"prev_page_url": null,
"to": 3,
"total": 3
}

我确实尝试过使用groupBy func,但这不是我所需要的。

我想要这样的结果;

{
"id": 3,
"name": "Muhammed Ali Yüce",
"username": "ali",
"avatar": "1544128196.png",
"stories": [
{
"id": 2,
"user_id": 3,
"image": "1550228567.jpg",
"created_at": "2019-02-15 11:02:47",
"updated_at": "2019-02-15 11:02:47"
},
{
"id": 3,
"user_id": 3,
"image": "1550228567.jpg",
"created_at": "2019-02-15 11:02:47",
"updated_at": "2019-02-15 11:02:47"
}
]

StoryController.php

<?php

namespace App\Http\Controllers;
use App\Story;
use App\User;
use Illuminate\Http\Request;
use Image;
use Illuminate\Support\Facades\DB;
use Illuminate\Pagination\LengthAwarePaginator;

class StoryController extends Controller
{

  public function index(Request $request){

    $userId = $request->id;

    $followsArr = [$userId];
    $follows = DB::table('follows')->whereNotIn('ismuted', [1])->where('follower', $userId)->get();

    foreach ($follows as $follow) {
      $friend = $follow->following;
      $followsArr[] = $friend;
    }

    $itemCollection = collect($followsArr);

      $stories = [];

      Story::whereIn('user_id', $itemCollection)
      ->orderBy('created_at', 'DESC')
      ->get()->each(function ($story) use (&$stories){

        $user = User::where('id', $story->user_id)->first();

        $stories[] =
          ['id' => $user->id]
        + ['name' => $user->name]
        + ['username' => $user->username]
        + ['avatar' => $user->avatar]
        + ['stories' => $story->toArray()];

      });


      // Get current page form url e.x. &page=1
              $currentPage = LengthAwarePaginator::resolveCurrentPage();

              // Create a new Laravel collection from the array data
              $itemCollection = collect($stories);

              // Define how many items we want to be visible in each page
              $perPage = 10;

              // Slice the collection to get the items to display in current page
              $currentPageItems = $itemCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->all();

              // Create our paginator and pass it to the view
              $paginatedItems= new LengthAwarePaginator($currentPageItems , count($itemCollection), $perPage);


      return response()->json($paginatedItems);

  }

我希望你能理解我的意思,在此先感谢:)

1 个答案:

答案 0 :(得分:1)

假设您有User.phpStory.php模型

在User.php中

public function stories()
{
   return $this->hasMany(Story::class);
}

您可以与用户一起加载故事。像这样的东西

$user = User::where('id', $story->user_id)->with('stories')->first(); //Eager Load

or 

$user = User::where('id', $story->user_id)->first();
$user->load('stories'); //Lazy Eager Load

请详细了解Laravel Eloquent: Relationships