I'm building 2 websites, one of them will be hosted on the server and the other one on localhost, both have their own database. The one on the server has API routes getting data from client and insert it to database. The function i'm trying to implement is that the one on localhost will be retrieving data from its own database, then call the API route of the other one on the server then inserting it to the server's database.
expected result : Data from localhost database inserted into the server's database via API.
actual result : The server is inserting the data back into the localhost database.
question : Is there a way to force the server to insert the data to it's own database?
codes : Localhost function
public function Sync (){
$payload = t_worksheet::all()->toArray();
$client = new Client();
$res = $client->request('POST', 'http://localhost/cal-work/public/api/sync', ['form_params' =>
['payload' => $payload]]);
}
Server function
public function sync (Request $request) {
$payload = $request->payload;
foreach ($payload as $item) {
$target = new t_worksheet_server;
$target->name = $item['name'];
$target->details = $item['details'];
$target->approved_by = $item['approved_by'];
$target->save();
}
return response()->json('success', 200);
}
Server API routes
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/sync', 'SyncController@sync');
Local .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ws-local
DB_USERNAME=root
DB_PASSWORD=12345678
Server .env
DB_CONNECTION=mysql
DB_HOST=db4free.net
DB_PORT=33060
DB_DATABASE=ws-server
DB_USERNAME=ckaryuusai
DB_PASSWORD=12345678
Thanks in advance
It worked, i served the server project with php artisan serve --port=8080
and changed the guzzle client to localhost:8080
, it worked fine
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments