Categories

Sunday, May 5, 2024
#919814419350 therichposts@gmail.com
Laravel 8Reactjs

How to use reactjs as frontend and laravel 8 as backend?

How to use reactjs as frontend and laravel 8 as backend?

Hello friends, welcome back to my blog. Today in this blog post, I am going to tell you, How to use reactjs as frontend and laravel 8 as backend?

Reactjs Laravel

For reactjs new comers, please check the below link for basic understanding:

Reactjs Basic Tutorials


Here is the working code snippet for How to use reactjs as frontend and laravel 8 as backend? and please use carefully and avoid mistakes:

1. Firstly friends we need reactjs fresh setup and for then we need to run below commands into our terminal and also we should have latest node version installed on our pc:

npx create-react-app reactlaravel

cd reactlaravel

npm start

2. Now friends we need to run below commands also to have  axios module into our reactjs application:

npm install axios //API Service

npm start //start the application

3. Now, we need to add below code into our src/app.js file or you can replace below code with existing one that you have into your src/app.js file:

import React from 'react';
import './App.css';


//Importing axios service
import axios from 'axios';

class App extends React.Component {
  //initialize array variable
  constructor() {

    //super is used to access the variables
    super();
    this.state = {
       data: []
    }
 }
 componentDidMount() {

 //API request
 axios.get("http://localhost/therichpost/public/api/sample-restful-apis").then(response => {
  
  //getting and setting api data into variable
  this.setState({ data : response.data });
 
})
}
  
//Final output
render() {
  return (
    <div className="App">
      
        <h1>Therichpost.com</h1>
      
         <table>
          <thead>
            <tr>
              <th>Name</th>
              <th>Website</th>
             
            </tr>
          </thead>
          <tbody>
          {this.state.data.map((result) => {
            return (

            <tr>
              <td>{result.name}</td>
              <td>{result.domain}</td>
            
            </tr>
            )})}
           
          </tbody>
        </table>
    </div>
  );
  
}

}
export default App;

4. Laravel 8 demo api that I have made into my routes/api.php file:

Route::get('sample-restful-apis', function()
{
    return response()->json([
        [
            'name' => 'Therichpost',
            'domain' => 'therichpost.com'
        ],[
            'name' => 'Therichpost',
            'domain' => 'therichpost.com'
        ],[
            'name' => 'Therichpost',
            'domain' => 'therichpost.com'
        ],[
            'name' => 'Therichpost',
            'domain' => 'therichpost.com'
        ],[
            'name' => 'Therichpost',
            'domain' => 'therichpost.com'
            ]
        ]);
});

Now we are done friends. If you have any kind of query or suggestion or any requirement then feel free to comment below.

Guys in this post, I am getting data into my reactjs front-end from Laravel 8 backend.

Note: Friends, I just tell the basic setup and things, you can change the code according to your requirements. For better understanding must watch video above.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad.

Jassa

Thanks

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.