0

I am trying to use trait because I will put the code in several files as it is an api connection. But when calling the variable that displays the values returned by the api, it is showing undefined in the controller.

App/Http/Traits/UserConnect.php

namespace App\Http\Traits;

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;

trait UserConnect
{

    public function connectInfo(Request $request)
    {
        try {
            $client = new \GuzzleHttp\Client();
            $url = "api_url";
            $response = $client->request('GET', $url, [
                'headers' => [
                    'Authorization' => 'Bearer '.$token,
                ],
            ]);
            $result = json_decode($response->getBody());

            return $result;

        }catch (\Exception  $e){
            dd($e);
        }
    }

}

Controller

<?php
namespace App\Http\Controllers;

use App\Http\Traits\UserConnect;

class HomeController extends Controller
{
    use UserConnect;

    public function page(Request $request)
    {
        $api = $this->connectInfo($result);
        dd($api);
    }

$result is returning undefined but $ api is forcing 1 variable.

2
  • you are sending $result while you just have $request Commented Jun 3, 2020 at 18:37
  • $api = $this->connectInfo($request); Commented Jun 3, 2020 at 18:39

1 Answer 1

1

Your call should be $api = $this->connectInfo($result$request); because your function declaration accepts Request public function connectInfo(Request $request)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.