mostafalaravel
5 months ago

How to custom "With()" in relationship models ?

Posted 5 months ago by mostafalaravel

Hello ,

Here the index() controller :

        $companies = TeamleaderCompany::
        with(
            [
                'teamleaderCompanyEmails' => function ($query) {
                    $query->select('*');
                },

                'teamleaderCompanyTags' => function ($query) {
                    $query->select('*');
                },

                'teamleaderCompanyTelephones' => function ($query) {
                    $query->select('*');
                },


            ]
        )->orderBy($sorting, $sortorder)
            ->paginate($limit);

        return $companies;

it works , and as you can see the teamleader_company_tags is an array of objects, I would like to get only the tag value then implode them as string to be like : hello, coucou, dell

the result is :

"data": [
    {
      "id": 2,
      "teamleader_id": null,
      "created_at": "2019-08-04 15:08:16",
      "updated_at": "2019-08-04 15:08:16",
      "name": "abc",
      "vat_number": null,
      "national_identification_number": null,
      "website": null,
      "iban": null,
      "bic": null,
      "language": null,
      "remarks": null,
      "marketing_mails_consent": false,
      "deleted_at": null,
      "teamleader_company_emails": [
        {
          "id": 1,
          "email": "[email protected]",
          "type": "primary",
          "teamleader_company_id": 2,
          "created_at": "2019-08-04 15:13:33",
          "updated_at": "2019-08-04 15:13:33"
        }
      ],
      "teamleader_company_tags": [
        {
          "id": 1,
          "tag": "hello",
          "teamleader_company_id": 2,
          "created_at": "2019-08-05 10:58:35",
          "updated_at": "2019-08-05 10:58:35"
        },
        {
          "id": 2,
          "tag": "coucou",
          "teamleader_company_id": 2,
          "created_at": "2019-08-05 10:58:35",
          "updated_at": "2019-08-05 10:58:35"
        },
        {
          "id": 3,
          "tag": "dell",
          "teamleader_company_id": 2,
          "created_at": "2019-08-05 10:58:35",
          "updated_at": "2019-08-05 10:58:35"
        }
      ],
      "teamleader_company_telephones": [
        {
          "id": 1,
          "type": "sec",
          "number": "123456",
          "teamleader_company_id": 2,
          "created_at": "2019-08-05 10:40:03",
          "updated_at": "2019-08-05 10:40:03"
        },
        {
          "id": 2,
          "type": "sec",
          "number": "123456",
          "teamleader_company_id": 2,
          "created_at": "2019-08-05 10:44:15",
          "updated_at": "2019-08-05 10:44:15"
        },
        {
          "id": 3,
          "type": "sec",
          "number": "123456",
          "teamleader_company_id": 2,
          "created_at": "2019-08-05 10:48:14",
          "updated_at": "2019-08-05 10:48:14"
        },
        {
          "id": 4,
          "type": "sec",
          "number": "123456",
          "teamleader_company_id": 2,
          "created_at": "2019-08-05 10:53:06",
          "updated_at": "2019-08-05 10:53:06"
        },
        {
          "id": 5,
          "type": "sec",
          "number": "123456",
          "teamleader_company_id": 2,
          "created_at": "2019-08-05 10:53:50",
          "updated_at": "2019-08-05 10:53:50"
        },
        {
          "id": 6,
          "type": "sec",
          "number": "123456",
          "teamleader_company_id": 2,
          "created_at": "2019-08-05 10:57:29",
          "updated_at": "2019-08-05 10:57:29"
        },
        {
          "id": 7,
          "type": "sec",
          "number": "123456",
          "teamleader_company_id": 2,
          "created_at": "2019-08-05 10:58:35",
          "updated_at": "2019-08-05 10:58:35"
        }
      ]
    },
    {
      "id": 102,

Please sign in or create an account to participate in this conversation.