如何在API中更改响应

时间:2019-01-17 07:25:31

标签: laravel

我想在API中更改响应。我得到用户对象作为响应,但是我想要那里的连接对象,就像这里是我获取用户对象的Activity Profile Controller的代码。

当前我收到此回复。

"user": {

    "id": 1,

    "facebook_id": null,

    "name": "umar",

    "surname": "javed",

    "email": "uj1@gmail.com",

    "username": null,

    "date_of_birth": "1993-01-18",

    "profile_picture": null,

    "occupation": null,

    "gender": null,

    "city": "lahore",

    "country": null,

    "course": null,

    "university": null,

    "discription": null,

    "visible_on_feeds": 1,

    "visible_on_guestlists": 1,

    "created_at": "2018-05-02 10:35:03",

    "updated_at": "2019-01-15 14:32:54"

}

公共函数joinActivity(Activity $ activity)

{

    $authUser = JWTAuth::parseToken()->toUser();

    $userAge = Carbon::parse($authUser->date_of_birth)->age;

    if($activity->age_from <= $userAge && $activity->age_to >= $userAge)
    {
        $authUser->joinActivity($activity, $authUser);

        if ($activity->user_id != $authUser->id) {
            $user = User::where('id', $activity->user_id)->first();
            $user->notify(new JoinedTheActivity($authUser, $activity));
        }

        $userFriendIds = $authUser->friendslist()->pluck('id')->toArray();
        $joinedIds = $activity->joins()->pluck('user_id')->toArray();

        $joinedUsers = User::whereIn('id', $joinedIds)->whereIn('id', $userFriendIds)
            ->where('id', '!=', $authUser->id)->get();

        foreach ($joinedUsers as $joinedUser) {
            $joinedUser->notify(new FriendJoinedSameActivity($authUser, $activity));
        }

        return response()->json(['user'=> $authUser], 200);
    } else {

        return response()->json(['message'=> 'Your are not under the age limit of this Activity']);
    }
}

这是我的ActivityInterest特性的代码:

公共函数joinActivity(Activity $ activity,User $ user)

{

    if ( $this->isJoiningActivity($activity, $user))

    {

        $activity->interests()->update(['status'=> Status::JOINED]); 

    } else {

        $joining = $activity->interests()->create([

        'user_id' => $user->id,

        'activity_id' => $activity->id,

        'status' => Status::JOINED,

        ]); 

    return $joining->save();

    }

}

但是我想要这个答复我该怎么做请帮助我。预先感谢。

“加入”:[

            {

                "id": 71,

                "user_id": 1,

                "activity_id": 7,

                "status": 1,

                "created_at": "2018-12-21 07:05:30",

                "updated_at": "2018-12-21 07:05:30",

                "user": {

                    "id": 1,

                    "facebook_id": null,

                    "name": "umar",

                    "surname": "javed",

                    "email": "uj1@gmail.com",

                    "username": null,

                    "date_of_birth": "1993-01-18",

                    "profile_picture": null,

                    "occupation": null,

                    "gender": null,

                    "city": "lahore",

                    "country": null,

                    "course": null,

                    "university": null,

                    "discription": null,

                    "visible_on_feeds": 1,

                    "visible_on_guestlists": 1,

                    "created_at": "2018-05-02 10:35:03",

                    "updated_at": "2019-01-15 14:32:54"

                },

                "activity": {

                    "id": 7,

                    "activity_type": "event",

                    "user_id": 1,

                    "category_id": 2,

                    "subcategory_id": 3,

                    "activity_privacy": "open",

                    "activity_privacy_visible": 1,

                    "activity_datetime_from": "2018-07-28 13:35:00",

                    "activity_datetime_to": "2018-12-12 04:10:12",

                    "activity_address": "lahore",

                    "company_id": 3,

                    "latitude": null,

                    "longitude": null,

                    "age_from": 16,

                    "age_to": 30,

                    "people_limit": "4",

                    "activity_picture": null,

                    "activity_title": "party",

                    "activity_description": "party bla bla",

                    "created_at": "2018-05-03 10:49:48",

                    "updated_at": "2018-09-29 07:57:10",

                    "user": {

                        "id": 1,

                        "facebook_id": null,

                        "name": "umar",

                        "surname": "javed",

                        "email": "uj1@gmail.com",

                        "username": null,

                        "date_of_birth": "1993-01-18",

                        "profile_picture": null,

                        "occupation": null,

                        "gender": null,

                        "city": "lahore",

                        "country": null,

                        "course": null,

                        "university": null,

                        "discription": null,

                        "visible_on_feeds": 1,

                        "visible_on_guestlists": 1,

                        "created_at": "2018-05-02 10:35:03",

                        "updated_at": "2019-01-15 14:32:54"

                    },

                    "subcategory": {

                        "id": 3,

                        "category_id": 2,

                        "subcategory_name": "party",

                        "subcategory_picture": "dds",

                        "created_at": null,

                        "updated_at": null,

                        "category": {

                            "id": 2,

                            "category_name": "nightlife",

                            "category_picture": "knk",

                            "category_color": "#992233",

                            "created_at": null,

                            "updated_at": "2018-08-09 11:21:23"

                        }

                    }

                }

            }

        ] 

1 个答案:

答案 0 :(得分:0)

当您需要在api中更改响应时,可以创建资源。这是正确的方法,将帮助您以所需的方式返回信息。

例如arc graft

更多信息在这里: https://laravel.com/docs/5.7/eloquent-resources