0

I create two laravel projects. One is for backend connect with database and second project is for frontend. And then i create route API from projectone (backend project) get data from database and pass to json. and i want projecttwo(frontend) get the data from api url.

i try this code:

My API route in first project:

Route::get('test',function(){
    $response=DB::table('student_tbl')->select('title')->get();
    return response()->json($response,200);
});

My Secondproject:

$client = new \GuzzleHttp\Client();        
        // Create a request
        $request = $client->get('http://localhost/myfirstproject/public/api/test');
        // Get the actual response without headers
        $response = $request->getBody();
        $json_decode=json_decode($response,true);
        foreach ($json_decode as $key => $value) {
            echo $value['title'].'<br>';
        } 

Buti get 500 internal server error.

How to fixed this?

11
  • 1
    Take a look in the laravel.log of your api project and see if the full error message is there. Commented Aug 16, 2018 at 16:50
  • 1
    @JamesCook when i change .env secondproject database the same with firstproject i can get the result Commented Aug 16, 2018 at 16:52
  • 1
    If I've understood your setup correctly the database configuration of your front end project shouldn't effect the responses you get from the api. Can you navigate to localhost/myfirstproject/public/api/test in your browser what do you get there? Commented Aug 16, 2018 at 16:55
  • 1
    yes i get all the result from my firstproject api route. Commented Aug 16, 2018 at 16:57
  • Do you have a more detailed error message for your second project than 500 internal error? Commented Aug 16, 2018 at 16:59

4 Answers 4

1

https://github.com/laravel/framework/issues/19454#issuecomment-305955056

based on the comment, you need to run php artisan config:cache to save the config for project

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

Comments

0

Try this to get the contents from your request. Since you are receiving the API response while you are trying from the browser, it seems there can be an issue with the second project.

You have to get the contents before passing the value for JSON decoding.

$response = $request->getBody()->getContents();

7 Comments

Illuminate\Database\QueryException: SQLSTATE[42S02]: Base table or view not found: 1146 Table &#03 (truncated...)
so i must configure that two project env the same??
Are you still getting the response while navigating this link in your browser? http://localhost/myfirstproject/public/api/test
when i configure env secondproject the same firstproject i can get data
try dd($response) before this line $json_decode=json_decode($response,true); and what is the response? Seems like this part of the code is working fine after the above changes and something else in the second project (other than the pasted code) is breaking.
|
0

Base table or view not found means database was not found

Run migrations with

 php artisan migrate 

If you face other errros while running migrations then try this command

php artisan migrate:refresh

write your code in try catch like this:

try{

  $client = new \GuzzleHttp\Client();        
    // Create a request
    $request = $client->get('http://localhost/myfirstproject/public/api/test');
    // Get the actual response without headers
    $response = $request->getBody();
    $json_decode=json_decode($response,true);
    foreach ($json_decode as $key => $value) {
        echo $value['title'].'<br>';
    } 

 }catch(\Exception $e){

    \Log::error( $e->getMessage().' '. $e->getLine() .' '. $e->getFile());
    return $e->getMessage().' '. $e->getLine() .' '. $e->getFile(); 

 }

Also, open your browser's developer console and open the network tab. Refresh your page and find something matching to your route in network controller. It will be red since you are getting error. click on it and see what it says. If your don't understand then just paste that response in your question

3 Comments

yes i got an error : Server error: GET http://localhost/myfirstproject/public/api/test resulted in a 500 Internal Server Error response:
See my answer and update your second project code it will tell you the error and line no of the file.
open your browser's developer console and open the network tab. Refresh your page and find something matching to your route in network controller. It will be red since you are getting error. click on it and see what it says. If your don't understand then just paste that response in your question
0

Error code 500 basically means you have something wrong with your code. Try this turn one your application debug mode from .env if it is already there then its good.

Then try to hit the api from the browser check if that is giving /returning the response as expected. If that does then something is wrong with your 2ned project. Try to debug it same way .

The laravel error will give you a good hint where it is going wrong.

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.