Petyo Tsonev
Petyo Tsonev

Reputation: 587

Laravel Eloquent relationship keyBy()

i want to modify the result set of relationship in the following way

I have this array


    array:11 [▼
      "id" => 1
      "user_id" => 1
      "name" => "Test Case"
      "created_at" => "2017-08-25 17:12:26"
      "updated_at" => "2017-08-29 11:59:46"
      "hashid" => "5LOkAdWNDqgVomK"
      "user" => array:8 [▶]
      "order" => array:8 [▶]
      "classification" => array:2 [▼
        0 => array:9 [▼
          "id" => 1
          "case_id" => 1
          "method" => "facial_map_manual"
          "strong" => 2.0
          "dynamic" => 30.0
          "delicate" => 50.0
          "calm" => 18.0
          "created_at" => "2017-08-31 11:00:00"
          "updated_at" => "2017-08-31 11:00:00"
        ]
        1 => array:9 [▼
          "id" => 2
          "case_id" => 1
          "method" => "interview"
          "strong" => 20.0
          "dynamic" => 25.0
          "delicate" => 30.0
          "calm" => 25.0
          "created_at" => "2017-08-31 11:00:00"
          "updated_at" => "2017-08-31 11:00:00"
        ]
      ]
      "classification_data" => array:3 [▼
        0 => array:6 [▼
          "id" => 1
          "case_id" => 1
          "type" => "facial_points"
          "data" => null
          "created_at" => "2017-08-31 11:00:00"
          "updated_at" => "2017-08-31 11:00:00"
        ]
        1 => array:6 [▼
          "id" => 2
          "case_id" => 1
          "type" => "interview"
          "data" => array:4 [▶]
          "created_at" => "2017-08-31 12:00:00"
          "updated_at" => "2017-08-31 11:00:00"
        ]
        2 => array:6 [▶]
      ]
      "teeth_configuration" => array:6 [▶]
    ]

I want to map classification and classification_data by column key ( type OR method ) which are loaded relationships. This is the query:

 $case = RCase::with(['user','order','classification','classificationData','teethConfiguration'])->where('id',$id)
                    ->first();

This is what i tried so far

$case->classificationData =  $case->classificationData->keyBy('type')->toArray();

With the first method they are ordered by type but the old classification_data stays there even if try to

 unlink($case->classification_data) 
This is not the big problem... I can do this also with classification but the data stays the same.

Is there any method with which i can do keyBy() straight in the loaded relation model ? Thanks in advance

PHP Version: 7.1
Laravel Version: 5.4.38
OS: Ubuntu 16.04 LTS

Upvotes: 6

Views: 11764

Answers (1)

Arthur Samarcos
Arthur Samarcos

Reputation: 3299

You can replace the relation with this code:

$case->setRelation("classification_data",$case->classificationData->keyBy('type'));

To replace the relation you must provide the same relation name or laravel will create another one.

When you do:

$case->classificationData = something

You are actually adding a new attribute to the model object.

Upvotes: 3

Related Questions