Search code examples
phpmysqllaravelapicontroller

How to display data from API and Database in the same page in laravel?


I am working on an API and MySQL project that will save some of the data in MySQL and some in the API

Controller (index function):

{
    $response = Http::post('http://example.com/authenticate', [
        'Username' => 'ADMIN',
        'Password' => 'ADMIN',
        'Token' => 'FK98D...',
    ]);
    $token = json_decode($response, true);


    $apiURL = 'http://example.com/api/SalesOrder/';
    $headers = [
        'Content-Type' => 'application/json',
        'Authorization' => $token,
    ];
    $response2 = Http::withHeaders($headers)->get($apiURL);
    $data = $response2->json();
    $jobdetail = JobDetail::all();
    return view('api.auth.orders.index', compact('data','jobdetail'));
}

the above function is working correctly

Controller (store function):

public function store(Request $request)
{

    $response = Http::post('http://example.com/authenticate', [
        'Username' => 'ADMIN',
        'Password' => 'ADMIN',
        'Token' => 'FK98D...',
    ]);
    $token = json_decode($response, true);

    $request->validate([
        'job_order_no' => 'required',
        'sap_no' => 'required',
        'pic_name' => 'required',
    ]);
    JobDetail::create($request->all());

    $store = Http::withHeaders([
        'Content-Type' => 'application/json',
        'Authorization' => $token,
    ])->post('http://example.com/api/SalesOrder/', [
        
        'DocNo' => $request->job_order_no,
        'TotalQty' => $request->TotalQty,
        'TotalTransferredAOQty' => $request->TotalTransferredAOQty,
        'SODTL' => array([
            'DtlKey' => "",
            'ItemCode' => $request->ItemCode,
            ])

            ]);

            return $store;
}

and the above function is storing data to API and MySQL note: that 'DocNo' is using the 'job_order_no' request so both will be the same value to be able to call it for show function (i am not sure if this is the best approach)

Controller (show function):

public function show($DocNo,JobDetail $company)
{
    $client = new Client();
    $headers = [
        'Content-Type' => 'application/x-www-form-urlencoded'
    ];
    $options = [
        'form_params' => [
            'Username' => 'ADMIN',
            'Password' => 'ADMIN',
            'Token' => 'FK98DL...'
        ]
    ];
    $request = new Psr7Request('POST', 'http://example.com/authenticate', $headers);
    $res = $client->sendAsync($request, $options)->wait();
    $token = json_decode($res->getbody(),true);



    $client = new Client();
    $headers = [
        'Authorization' => $token,
        'Content-Type' => 'application/x-www-form-urlencoded'
    ];
    $options = [
        'form_params' => [
            'DocNo' => $DocNo
        ]
    ];
    $request = new Psr7Request('GET', 'http://example/api/SalesOrder/GetSalesOrder/', $headers);
    $res = $client->sendAsync($request, $options)->wait();
    $data = json_decode($res->getBody(),true);
    return view('api.auth.orders.show', compact('data','company'));
}

view (to redirect to show page):

<td class="text-center"><a href="{{ route('orders.show', $item['DocNo']) }}"></a></td>

how to redirect the above "a" tag to get the data from API and MySQL from 'DocNo' ('DocNo' is 'job_order_no' in MySQL as i mentioned above in store function)

is there a query that i need to add to show function to get data from database where the DocNo from the API equals DocNo from MySQL?


Solution

  • SOLUTION:

    i used query to get the same value from MySQL as showing below:

    $jobs = DB::table('jobdetails')->where('job_order_no', $DocNo)->first();