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

kingsleyuchenna's avatar

Local API returning internal data instead of external data

I have two Laravel projects, in this context, I will call project A and project B. The two projects have Users mode. I created an API in Project A that will retrieve users' details that will be consumed by project B. On project B, when I call the API, it never leaves Project B, every data returned by the API is still Project B. Example: Project A = Users: [user1, user2, user3, ..., user n] Project B = Users: [] Project B calls API to retrieve users from Project A, data returned will be [].

Help, thanks

0 likes
17 replies
OussamaMater's avatar

Maybe provide some code on how you are consuming the project's A API?

1 like
kingsleyuchenna's avatar

@OussamaMater

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Http;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    $urlQuery = 'http://site.test/api/account-retrieve/username/password';
    return response()->json(['status' => false, 'msg' => Http::get($urlQuery)->json()]);
});


OussamaMater's avatar

@kingsleyuchenna yes I know but at what test domain, like project B site.test how about project A? maybe this is where the confusion coming from, specially if both projects have the same endpoint, you can't tell with a 404 response

1 like
kingsleyuchenna's avatar

@OussamaMater this for project A Route

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});
Route::get('/account-retrieve/{username}/{password}', [\App\Http\Controllers\Site\NoPageController::class, 'ApiRetrieveAccount']);

Controller

<?php

namespace App\Http\Controllers\Site;

use App\Models\User;
use Illuminate\Http\Request;

class NoPageController extends SiteController
{

    //    APIS
    public function ApiRetrieveAccount($username, $password)
    {
        $select = [
            'id', 'name', 'username', 'password',
        ];

        if (!$user = User::query()->select($select)->whereUsername($username)->first()) {
            return response()->json(['status' => 'failed', 'message' => "Invalid User!"], 403);
        }
//        check password
        if (!\Hash::check($password, $user->password))
            return response()->json(['status' => 'failed', 'message' => "Invalid Password!"], 403);

        return response()->json(['status' => 'success', 'message' => "Confirmed.", 'user' => $user]);
    }
}
ryangurnick's avatar

I think what @oussamamater is asking, is if you are using laravel valet and if so what are the .test domains that are bound to each project.

If you are using valet you can run in the following command to figure that out: valet links

kingsleyuchenna's avatar

I have tried testing the API with API tester and it worked, vanilla PHP using file_get_contents and it worked. But if I tried using it within a Laravel app it will not return the right data.

OussamaMater's avatar

@kingsleyuchenna Well could be anything really, if you have both projects hosted somewhere, like Github, I will have a look, and try setting them both locally, because it's not a Laravel thing so far, I think it's confusing in domains, specially that you have similar endpoints.

1 like
kingsleyuchenna's avatar

@OussamaMater I think my .env file is the issue. When I call the Project A API from Project B, Project B .env replaces Project A .env

Please or to participate in this conversation.