Fateh Alrabeai
Fateh Alrabeai

Reputation: 730

how to change object name into different name when using laravel eloquent relationship?

I want to change the courses object's name from "courses" to "levels" in the response via the eloquent query or if there's a way to do so. Because I don't have much time to change The whole project since the project is already live but now working on API for a mobile developer and I don't want to be there any confusion with names because of my client made me change many things when I was building the project;

This is my eloquent code:

 $languageLevels = Category::with('courses')->get();

  return response()->json(['code'=>1,'success'=>true, 'languageLevels'=>$languageLevels]);

The response:

{
    "code": 1,
    "success": true,
    "languageLevels": [
        {
            "id": 25,
            "name": "German",
            "status": 1,
            "created_at": "2020-10-10T08:16:21.000000Z",
            "updated_at": "2020-10-10T18:35:34.000000Z",
            "courses": [
                {
                    "name": "Beginners",
                    "content": "Beginner level",
                    "image_name": "1602175010.jpg",
                    "uuid": "1887da0c-0864-4d8c-9447-e427c06085ae",
                    "status": 0,
                    "category_id": 25,
                    "created_at": "2020-10-08T16:36:50.000000Z",
                    "updated_at": "2020-11-10T15:34:07.000000Z"
                }
            ]
        },
        {
            "id": 24,
            "name": "ُEnglish",
            "status": 1,
            "created_at": "2020-10-08T16:34:32.000000Z",
            "updated_at": "2020-10-08T16:34:32.000000Z",
            "courses": [
                {
                    "name": "Intermediate",
                    "content": "Intermediate",
                    "image_name": "1602181221.jpg",
                    "uuid": "2dcb6d86-ded0-4419-bf1a-7c30ecf6f4fb",
                    "status": 1,
                    "category_id": 24,
                    "created_at": "2020-10-08T18:20:21.000000Z",
                    "updated_at": "2020-10-08T18:20:21.000000Z"
                }
            ]
        }
    ]
}

And this is How I want it to be like: from "courses": [ to "levels": [

{
    "code": 1,
    "success": true,
    "languageLevels": [
        {
            "id": 25,
            "name": "German",
            "status": 1,
            "created_at": "2020-10-10T08:16:21.000000Z",
            "updated_at": "2020-10-10T18:35:34.000000Z",
            "levels": [
                {
                    "name": "Beginners",
                    "content": "Beginner level",
                    "image_name": "1602175010.jpg",
                    "uuid": "4444-e427c06085ae",
                    "status": 0,
                    "category_id": 25,
                    "created_at": "2020-10-08T16:36:50.000000Z",
                    "updated_at": "2020-11-10T15:34:07.000000Z"
                }
            ]
        },
        {
            "id": 24,
            "name": "ُEnglish",
            "status": 1,
            "created_at": "2020-10-08T16:34:32.000000Z",
            "updated_at": "2020-10-08T16:34:32.000000Z",
            "levels": [
                {
                    "name": "Intermediate",
                    "content": "Intermediate",
                    "image_name": "1602181221.jpg",
                    "uuid": "2dcb6d86-ded0-4419-bf1a-7c30ecf6f4fb",
                    "status": 1,
                    "category_id": 24,
                    "created_at": "2020-10-08T18:20:21.000000Z",
                    "updated_at": "2020-10-08T18:20:21.000000Z"
                }
            ]
        }
    ]
}
``

Upvotes: 1

Views: 1653

Answers (2)

Md. Salman Hider
Md. Salman Hider

Reputation: 11

In the Category model you can just add this.

    public function courses(){
        return $this->hasMany(Course::class, 'category_id');
    }

    public function levels(){
        return $this->courses();
    }

The method levels is same as courses. So you can use anyone of them if needed.

    $languageLevels = Category::with('courses')->get();

Or

    $languageLevels = Category::with('levels')->get();

Upvotes: 1

Maulik Shah
Maulik Shah

Reputation: 1050

You have to loop through each object and assign courses object to new key levels. But it will increase the execution time as you need to loop through each result.

Or you can use map also.

If speed is not an issue and don't have many records then you can use this approach or as alternative to that you can use eloquent resource as @DigitalDrifter suggest. So you have to change in that resource only instead of all places.

Eloquent Resources

Upvotes: 1

Related Questions