Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ldiebold's avatar

Response relations not loading when testing Laravel API

I have a simple API that returns a user with their farms.

public function index(Request $request)
{
    return User::where('admin', '!=', true)
        ->with('farms')
        ->get();
}

This works great, and gives the response you'd expect:

[
  {
    "id": 2,
    "admin": 0,
    "name": "Boss Smith",
    "email": "[email protected]",
    "agreed_to_terms": 0,
    "active": 1,
    "roles": [
      {
        "id": 1,
        "role": "business_manager",
        "user_id": 2
      }
    ],
    "farms": [
      {
        "Farm_Code": 273,
        "Farm_Name": "Myriam Hartmann",
        "pivot": {
          "user_id": 2,
          "details_id": 273,
          "is_farms_manager": null,
        }
      },
      {
        "Farm_Code": 564,
        "Farm_Name": "Brent Zieme DVM",
        "pivot": {
          "user_id": 2,
          "details_id": 564,
          "is_farms_manager": null
        }
      }
    ]
  },
...
]

The API works as expected... Except when I'm testing. For some strange reason, hitting the same enpoint in a test doesn't fetch relations. In other words, the result is always shallow.

Here's my test:

public function an_admin_can_fetch_a_client_with_their_farms()
{
    $admin_table_seeder = new \UserAdminTableSeeder;
    $client_table_seeder = new \UserClientTableSeeder;
    $details_table_seeder = new \DetailsTableSeeder;
    $associate_with_farms_table_seeder = new \AssociateClientsWithFarmsSeeder;

    $admin_table_seeder->run();
    $client_table_seeder->run();
    $details_table_seeder->run();
    $associate_with_farms_table_seeder->run();

    $this->loginAsAdmin();

    $response = $this->json("GET", "/api/clients", [
        'with' => ['farms']
    ]);

    $response->dump();

    $response->assertJsonStructure([
        '1' => [
            'name',
            'farms'
        ]
    ]);
}

And here's the dump:

array:3 [
  0 => {#2500
    +"id": 2
    +"admin": 0
    +"name": "Boss Smith"
    +"email": "[email protected]"
    +"email_verified_at": "2019-03-25 00:08:33"
    +"created_at": "2019-03-25 00:08:33"
    +"updated_at": "2019-03-25 00:08:33"
    +"agreed_to_terms": 0
    +"business_id": 1
    +"phone": null
    +"address_1": null
    +"address_2": null
    +"city": null
    +"state": null
    +"postcode": null
    +"deleted_at": null
    +"active": 1
  }
  1 => {#2516
    +"id": 3
    +"admin": 0
    +"name": "Worker Smith"
    +"email": "[email protected]"
    +"email_verified_at": "2019-03-25 00:08:33"
    +"created_at": "2019-03-25 00:08:33"
    +"updated_at": "2019-03-25 00:08:33"
    +"agreed_to_terms": 0
    +"business_id": 1
    +"phone": null
    +"address_1": null
    +"address_2": null
    +"city": null
    +"state": null
    +"postcode": null
    +"deleted_at": null
    +"active": 1
  }
  2 => {#2513
    +"id": 4
    +"admin": 0
    +"name": "Worker Smith 2"
    +"email": "[email protected]"
    +"email_verified_at": "2019-03-25 00:08:33"
    +"created_at": "2019-03-25 00:08:33"
    +"updated_at": "2019-03-25 00:08:33"
    +"agreed_to_terms": 0
    +"business_id": 1
    +"phone": null
    +"address_1": null
    +"address_2": null
    +"city": null
    +"state": null
    +"postcode": null
    +"deleted_at": null
    +"active": 1
  }
]

I've tried converting the response into json (Laravel does this automatically anyway)

return User::where('admin', '!=', true)
    ->with('farms')
    ->get()
    ->toJson();

I've tried making sure the data I'm testing with is the exact same (hence the excessive use of seeders)

I've even proven that the relationship exists when in a testing environment

$users = User::where('admin', '!=', true)
    ->with('farms')
    ->get();

dd($users[0]->farms->toJson());

returns the following in a testing environment:

[
   {
      "created_at":"2019-03-25 00:22:50",
      "updated_at":"2019-03-25 00:22:50",
      "Farm_Code":384,
      "Farm_Name":"Prof. Verla Bergnaum DDS",
      "Trading_Name":"Placeat saepe consequatur reprehenderit laborum quia.",
      "Contact_Name":"Aurelio Doyle",
      "Address_1":"Viviane Tunnel",
      "Address_2":"597 Emery Rapid Apt. 629",
      "City":"East Ransom",
      "State":"New Jersey",
      "Postcode":"57965-4264",
      "pivot":{
         "user_id":2,
         "details_id":384,
         "is_farms_manager":null,
         "created_at":"2019-03-25 00:22:50",
         "updated_at":"2019-03-25 00:22:50"
      }
   },
   {
      "created_at":"2019-03-25 00:22:50",
      "updated_at":"2019-03-25 00:22:50",
      "Farm_Code":99,
      "Farm_Name":"Zackary Stehr",
      "Trading_Name":"Qui fugiat voluptas.",
      "Contact_Name":"Cooper Gusikowski",
      "Address_1":"Schulist Mission",
      "Address_2":"691 Stoltenberg Run Suite 119",
      "City":"Melanyshire",
      "State":"Texas",
      "Postcode":"08426-2712",
      "pivot":{
         "user_id":2,
         "details_id":99,
         "is_farms_manager":null,
         "created_at":"2019-03-25 00:22:50",
         "updated_at":"2019-03-25 00:22:50"
      }
   }
]

Why doesn't the response include relations in a testing environment?

I've been at this for literally days and would really appreciate some help!

0 likes
1 reply
ldiebold's avatar
ldiebold
OP
Best Answer
Level 8

Had something to do with the version of phpunit I was using. If anyone has this problem, try using Laravels local install of phpunit

vendor/phpunit/phpunit/phpunit

Or maybe try updating your global composer dependencies

composer global update

Please or to participate in this conversation.