3

I have two laravel projects. One of them has an API

I am trying to get data using the API.

public function getSyncOrders() {
    $orders = Invoice::where('status', 0)->get();
    return response()->json([
        'data' => [
            'orders'                => $orders
        ]
    ], 200);      
}

I am trying to fetch data in the other laravel project.

public function syncOrders() {
    if(auth()->check()){
        Order::truncate();

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => "http://project1.net/api/sync-orders",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 600,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET"
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            //echo "cURL Error #:" . $err;
        } else {
            echo $response;
        }
    }
    else{
        return redirect('/');
    }
}

But I get the error :

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'project2.invoice' doesn't exist (SQL: select * from invoice where status = 0)

Project 1 has table invoice in database while project 2 has table orders in database.

If I use the url http://project1.net/api/sync-orders in browser, it returns data.

I need help to fix the curl request so that project 2 doesn't execute code and search in its own database but instead get the data from the API.

2
  • 1
    Have you managed to resolve this issue? I am having the same problem and I wouldn't like to add a second connection in my laravel configs Commented Oct 11, 2019 at 20:51
  • @JaimeNoelAlvarezLuna Yes, I didn't have to add a second connection, just had to rename the key in env and config for one of the two projects, or just directly specify db name in config. Usually renaming in env and config is a better option Commented Oct 24, 2021 at 10:42

4 Answers 4

5

Changing DB_DATABASE in config and env to DB_DATABASE2 for project 2 fixed the problem.

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

Comments

1

It is a cache problem, please run these commands in order to fix your problems:

php artisan config:clear
php artisan config:cache
php artisan cache:clear
php artisan optimize

Thanks to Sergio Chamorro for the solution.

Comments

0

It's so weird that you are seeing that error. If you insist on doing the job only using curl, maybe it'd be better if you provide more details.

But I recommend start using Guzzle

composer require guzzlehttp/guzzle:~6.0

then your code will become like this:

public function syncOrders() {
    if(auth()->check()){
        $client = new \GuzzleHttp\Client(['base_uri' => 'http://project1.net/api/']);
        $response = $client->get('sync-orders')->send();
        dd($response->getBody(), $response); // you can do whatever you want with $response
    }

    return redirect('/');

}

2 Comments

What more details do you need?
What servers project1.net & project2 are pointing to? are they on the same server? are you sure that you are getting the error while you are calling the second project's syncOrders? it's impossible for the syncOrders function to throw the error you said Table 'project2.invoice' doesn't exist because there invoice is not mentioned in it anywhere.
0

It seems in this url (http://project1.net/api/sync-orders) , Invoice model is connected to 'project2' database.

So in your .env file or your config in (config/database.php) change database to project1

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.