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

signalwarrant's avatar

Pass External API data to view

I have managed to get my External API call working, it yields these results using dd().

{#701 ▼ // app/Http/Controllers/DataController.php:21
  +"stats": array:4 [▼
    0 => {#705 ▼
      +"category_id": 1
      +"category": "Oval"
      +"starts": 128
      +"wins": 3
      +"top5": 38
      +"poles": 2
      +"avg_start_position": 9
      +"avg_finish_position": 10
      +"laps": 6267
      +"laps_led": 158
      +"avg_incidents": 6.3125
      +"avg_points": 36
      +"win_percentage": 2.34
      +"top5_percentage": 29.69
      +"laps_led_percentage": 2.52
      +"total_club_points": 0
      +"year": 2022
    }
    1 => {#681 ▶}
    2 => {#713 ▶}
    3 => {#697 ▶}

This is my Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Env;
use Illuminate\Support\Facades\Http;
use iRacingPHP\iRacing;

class DataController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function memberYearly()
    {
        $response = new iRacing(env('EMAIL'), env('PASSWORD'));
        $yearlyStats = $response->stats->member_yearly();
        dd($yearlyStats);        
    }
}

How do I pass the individual data elements from $yearlyStats to my view? I can't seem to get the syntax correct or find a suitable equivalent example on the Google machine.

0 likes
7 replies
martinbean's avatar

@signalwarrant Just pass it like any other view data?

$response = new iRacing(env('EMAIL'), env('PASSWORD'));
$yearlyStats = $response->stats->member_yearly();

return view('some.view', ['yearlyStats' => $yearlyStats]);

Also, you shouldn’t be using env outside of configuration files. And it’s probably better to bind an instance of that class to the container so you can inject it where you need, instead of manually instantiating it everywhere you’re going to use it:

$this->app->singleton(iRacing::class, function () {
    return new iRacing($this->app['config']['services.iracing.email'], $this->app['config']['services.iracing.password']);
});
class DataController extends Controller
{
    protected $service;

    public function __construct(iRacing $service)
    {
        $this->service = $service;
    }

    public function memberYearly()
    {
        return view('some.view', [
            'yearlyStats' => $this->service->states->member_yearly(),
        ]);
    }
}
signalwarrant's avatar

@martinbean Thank you for your reply.

Using the service provider, where does the actual email and password go in the code? Is best practice to add that code to one of the existing Service Providers or create a custom provider?

martinbean's avatar

@signalwarrant You can just add it to your AppServiceProvider

You register the binding once. You can then type-hint that in classes that need an instance, and Laravel’s service container will instead resolve the binding and inject it, so you don’t have to manually instantiate the class in multiple places.

martinbean's avatar

@signalwarrant Set the email and password as environment variables:

[email protected]
IGAMING_PASSWORD=hunter2

Then map them to configuration values:

// config/services.php
return [
    'igaming' => [
        'email' => env('IGAMING_EMAIL'),
        'password' => env('IGAMING_PASSWORD'),
    ],
];
signalwarrant's avatar

@martinbean I'm still not understanding the flow here. Apologies if these are Laravel 1st grade type questions. My username and password is still stored in the .env.

Where does this code go? In the register function of the AppServiceProvider?

$this->app->singleton(iRacing::class, function () {
    return new iRacing($this->app['config']['services.iracing.email'], $this->app['config']['services.iracing.password']);
});

This code is in config/services.php

// config/services.php
return [
    'iracing' => [
        'email' => env('IRACING_EMAIL'),
        'password' => env('IRACING_PASSWORD'),
    ],
];

This is my controller

<?php

namespace App\Http\Controllers;

use iRacingPHP\iRacing;

class iracingController extends Controller
{
    protected $service;

    public function __construct(iRacing $service)
    {
        $this->service = $service;
    }

    public function memberYearly()
    {
        $yearlyStats = $this->service->states->member_yearly();
        dd($yearlyStats);   
    }
}

martinbean's avatar
Level 80

@signalwarrant Yeah, the $this->app->singleton line would go in the register method of your AppServiceProvider class.

Please or to participate in this conversation.