How to display a table according to a foreign key which is display from another foreign key / Laravel 6

Tags: , ,



Model:

public function followup() 
{
   return $this->hasMany('AppFollowUp', 'id_log', 'id')
   ->select(['id', 'id_log', 'request', 'id_priority']);
}

follow_up.id_priority is a foreign key to priority.id

Controller:

$test = DmLog::withTrashed()
    ->with('followup')
    ->with('department')
    ->with('membership')
    ->with('room')
    ->with('roommove')
    ->with('communication')
    ->with('category')
    ->with('userCreator')
    ->with('userUpdator')
    ->find($id);

Piece of the json data I got returned :

[{...},
    "created_at": "2020-12-29T18:14:26.000000Z",
    "updated_at": null,
    "deleted_at": null,
    "followup": [
        {
            "id": 2,
            "id_log": 1,
            "request": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
            "id_priority": 2
        },
        {
            "id": 3,
            "id_log": 1,
            "request": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
            "id_priority": 1
        }
    ],
    "department": {
        "id": 2,
        "department": "Kitchen"
    },
{...}]

Could you please explain me how to display the table priority according to the foreign key which is id_priority ?

Okay… there’s a misunderstanding, so here’s the result i’m trying to get :

[{...},
    "created_at": "2020-12-29T18:14:26.000000Z",
    "updated_at": null,
    "deleted_at": null,
    "followup": [
        {
            "id": 2,
            "id_log": 1,
            "request": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
            "id_priority": 2
            "priority": {
               "id": 2
               "priority": "not urgent"
            }
        },
        {
            "id": 3,
            "id_log": 1,
            "request": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
            "id_priority": 1,
            "priority": {
               "id": 1
               "priority": "urgent"
            }
        }
    ],
    "department": {
        "id": 2,
        "department": "Kitchen"
    },
{...}]

Answer

You can call a closure function on with method:

use IlluminateDatabaseEloquentRelationsBelongsTo;

$test = DmLog::withTrashed()
    ->with([
        'followup' => function (BelongsTo $relationship) {
            $relationship->select(['id', 'priority']);
         },
    ]);


Source: stackoverflow