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

alya_alsiyabi's avatar

No data coming from API

I am practicing APi, i bulid 2 websites( jobs website, test website) in my local server, I want to take job data from first website and show them in my second test web site. I ran both of them The port of jobs website is http://127.0.0.1:8000 The port of test website is http://127.0.0.1:8080

The route in my api jobs website i

    Route::middleware('auth:api')->group(function () {
        Route::get('/jobs3', [JobApiController::class,'apijob']);
    });

Controller:

public function apijob()
        {
            $jobs=Job::all();
            // return view ('jobs',compact('jobs'));
    return $this->sendResponse(JobsResource::collection($jobs), 'jobs Retrieved Successfully.');
        }

        public function login(Request $request)
        {
            $input = $request->only(['email', 'password']);

            $validate_data = [
                'email' => 'required|email',
                'password' => 'required|min:8',
            ];

            $validator = Validator::make($input, $validate_data);
            
            if ($validator->fails()) {
                return response()->json([
                    'success' => false,
                    'message' => 'Please see errors parameter for all errors.',
                    'errors' => $validator->errors()
                ]);
            }
            if (auth()->attempt($input)) {
                $token = auth()->user()->createToken('passport_token')->accessToken;
                
                return response()->json([
                    'success' => true,
                    'message' => 'User login succesfully, Use token to authenticate.',
                    'token' => $token
                ], 200);
            } else {
                return response()->json([
                    'success' => false,
                    'message' => 'User authentication failed.'
                ], 401);
            }
        }
    public function logout()
    {
        $access_token = auth()->user()->token();

        // logout from only current device
        $tokenRepository = app(TokenRepository::class);
        $tokenRepository->revokeAccessToken($access_token->id);

        // use this method to logout from all devices
        // $refreshTokenRepository = app(RefreshTokenRepository::class);
        // $refreshTokenRepository->revokeRefreshTokensByAccessTokenId($$access_token->id);

        return response()->json([
            'success' => true,
            'message' => 'User logout successfully.'
        ], 200);
    }


    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required',
            'con_id' => 'required',

            'c_password' => 'required|same:password',
        ]);

        if($validator->fails()){
            return $this->sendError('Validation Error.', $validator->errors());       
        }

        $input = $request->all();
        $input['password'] = bcrypt($input['password']);
        $user = User::create($input);
        $success['token'] =  $user->createToken('MyApp')->accessToken;
        $success['name'] =  $user->name;

in the test website : route web Route::get('/Applyforjobs',[ConsumeApi::class,'index'])->name('Aman'); and controller:

       public function index(){
    $client = new Client();
    $url = "http://127.0.0.1:8000/api/jobs3";
    $params = [
            //If you have any Params Pass here
        ];
    $headers = [
            'api-key' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9. 
        ];
     $response = $client->request('GET', $url, [
            // 'json' => $params,
            'headers' => $headers,
            'verify'  => false,
        ]);
    $responseBody = json_decode($response->getBody());
// dd($responseBody);
        return view('jobs', compact('responseBody'));
    }

The error : no data is coming from api when I dd($responseBody);

0 likes
4 replies
Keshi's avatar

Are you getting any errors in the console? If so, please post it here

alya_alsiyabi's avatar

@Keshi

sir this is the error from console log :

[2022-08-02 06:28:15] local.ERROR: Invalid argument supplied for foreach() (View: C:\laravel\weather\resources\views\jobs.blade.php) {"view":{"view":"C:\laravel\weather esources\views/jobs.blade.php","data":{"errors":"<pre class=sf-dump id=sf-dump-671305523 data-indent-pad=" ">Illuminate\Support\ViewErrorBag {#281 #<span class=sf-dump-protected title="Protected property">bags: [] } Sfdump("sf-dump-671305523", {"maxDepth":3,"maxStringLength":160}) ","responseBody":"<pre class=sf-dump id=sf-dump-1294355058 data-indent-pad=" ">null Sfdump("sf-dump-1294355058", {"maxDepth":3,"maxStringLength":160}) "}},"exception":"[object] (Facade\Ignition\Exceptions\ViewException(code: 0): Invalid argument supplied for foreach() (View: C:\laravel\weather esources\views\jobs.blade.php) at C:\laravel\weather esources\views/jobs.blade.php:3) [stacktrace]

alya_alsiyabi's avatar
alya_alsiyabi
OP
Best Answer
Level 2

@Keshi Thx sir

I solved it the problem was in controller In the controller header I have to add Authorization in stead of api _key

Please or to participate in this conversation.