简体   繁体   中英

Response json in laravel

I have a four table in laravel framework:

USER: id_user, email, password, profile_image, fullname, gender, age, country of residence, city_if_residence,

EVENT id_event, id_user, name_event

MEMBER_EVENTS, id_member, id_user, id_event,

STORAGE id_storage, id_event, name_storage,

And I have a repsonse json :

{
    "success": true,
    "event": [
        {
            "id_event": 1,
            "id_user": 1,
            "name_event": "new event",
            "place_event": "Chocen, wladyslawa lokietka 424",
            "time_event": "19:00",
            "description": "description",
            "first_storage_file": "1598548420.jpg",
            "longitude": "124124",
            "latitude": "12312.123123",
            "storage_file": 1,
            "storage": [
                {
                    "id_storage": 1,
                    "id_event": 1,
                    "storage_file_title": "profile.jpg",
                    "created_at": null,
                    "updated_at": null
                }
            ]
        },
        {
            "id_event": 2,
            "id_user": 1,
            "name_event": "new event",
            "place_event": "Chocen, wladyslawa lokietka 424",
            "time_event": "19:00",
            "description": "description",
            "first_storage_file": "1598551003.jpg",
            "longitude": "124124",
            "latitude": "12312.123123",
            "storage_file": 0,
            "storage": []
        }
    ]
}

I would like to receive all the events , where In events will be all users Who belongs to events , and Data of the user who created event. For example :

{
    "success": true,
    "event": [
        {
            "id_event": 1,
            "id_user": 1,
            "name_event": "new event",
            "place_event": "Chocen, wladyslawa lokietka 424",
            "time_event": "19:00",
            "description": "description",
            "first_storage_file": "1598548420.jpg",
            "longitude": "124124",
            "latitude": "12312.123123",
            "storage_file": 1,
            "storage": [
                {
                    "id_storage": 1,
                    "id_event": 1,
                    "storage_file_title": "profile.jpg",
                    "created_at": null,
                    "updated_at": null
                }
            ]
        "users_member": [
                {
                    "id_user": 1,
                    "email": 1,
                    "profile_image": "profile.jpg",
                    "fullname": null,
                    "age": null
            "gender": null
                }
        {
                    "id_user": 1,
                    "email": 1,
                    "profile_image": "profile.jpg",
                    "fullname": null,
                    "age": null
            "gender": null
                }
        {
                    "id_user": 1,
                    "email": 1,
                    "profile_image": "profile.jpg",
                    "fullname": null,
                    "age": null
            "gender": null
                }
            ]
        "user_created_event": [
                {
                    "id_user": 1,
                    "email": 1,
                    "profile_image": "profile.jpg",
                    "fullname": null,
                    "age": null
            "gender": null
                }
            ]
        },
        {
            "id_event": 2,
            "id_user": 1,
            "name_event": "new event",
            "place_event": "Chocen, wladyslawa lokietka 424",
            "time_event": "19:00",
            "description": "description",
            "first_storage_file": "1598551003.jpg",
            "longitude": "124124",
            "latitude": "12312.123123",
            "storage_file": 0,
            "storage": []
        }
    ]
}

I created relationship in laravel.

USER Model :

public function event(){
        return $this->belongsToMany(Event::class,'member_events', 'id_user', 'id_event');
        //return $this->belongsToMany(Event::class,'member_events', 'id_event', 'id_event', 'id_event')->withTimestamps();
    }

EVENT MODEL

public function storage(){
        return $this->hasMany(StorageEvent::class, 'id_event');
    }
    
    public function user(){
        //return $this->belongsToMany(User::class,member_events, 'id_user', 'id_user')->withTimestamps();
        return $this->belongsToMany(User::class,'member_events', 'id_event', 'id_user');
    }

Your relationships are not correct.

Event should have a method

public function users()
{
    return $this->hasMany(User::class);
}

In User

public function event()
{
    return $this->belongsTo(Event:class);
}

seehttps://laravel.com/docs/7.x/eloquent-relationships#one-to-many

To have all of them on the response there is 2 ways :

  1. (Pro level)you append them to your response for a defined endpoint. this is the efficient way because you will have them only for the endpoints that you defined. To do it you use the property $appends in your model, and declare getUsersAttribute and getEventAttribute that return each the eloquent relation with a get() to have objects collection and not the builder.

  2. (Normal level)you can use the $with property, then they will be returned with your requested model

    protected $with = ['event','users'];

Check the Laravel documentation with your appropriate laravel version to see how to use the concepts that i mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM