Laravel 5 Simple Crud Application Using Reactjs Part 2
Laravel 5 Simple Crud Application Using Reactjs Part 2
com
Hardik Savani
10-12 m inutes
By Hardik Savani | December 10, 2017 | Category : PHP Laravel Javascript Bootstrap jQuery MySql JSON
Node JS Ajax React JS Axios
In the second link, we will follow rest of two step for react js crud tutorial application example. So let's start to
follow 2 step in this tutorial page for laravel react native and here we will generate components file for react
server side rendering.
First things is we need to configure our react app, So we need to run following bellow path for React Preset.
Next we need to run following command for install node js so let's run bellow command:
npm install
One thing is left we need to install one dependency so let's run following command for install it.
Now in this step, we require to create following files for react js code, i listed bellow all files:
1) app.js
2) bootstrap.js
3) components/CreateProduct.js
4) components/DisplayProduct.js
5) components/MasterProduct.js
6) components/MyGlobleSetting.js
7) components/TableRow.js
8) components/UpdateProduct.js
resources/assets/js/app.js
require('./bootstrap');
render(
<Router history={browserHistory}>
</Route>
</Router>,
document.getElementById('crud-app'));
resources/assets/js/bootstrap.js
window._ = require('lodash');
try {
require('bootstrap-sass');
} catch (e) {}
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
resources/assets/js/components/CreateProduct.js
constructor(props){
super(props);
this.handleChange1 = this.handleChange1.bind(this);
this.handleChange2 = this.handleChange2.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
handleChange1(e){
this.setState({
productTitle: e.target.value
})
handleChange2(e){
this.setState({
productBody: e.target.value
})
handleSubmit(e){
e.preventDefault();
const products = {
title: this.state.productTitle,
body: this.state.productBody
}
browserHistory.push('/display-item');
});
render() {
return (
<div>
<h1>Create Product</h1>
<form onSubmit={this.handleSubmit}>
<div className="row">
<div className="col-md-6">
<div className="form-group">
<label>Product Title:</label>
</div>
</div>
</div>
<div className="row">
<div className="col-md-6">
<div className="form-group">
<label>Product Body:</label>
</div>
</div>
</div><br />
<div className="form-group">
</div>
</form>
</div>
resources/assets/js/components/DisplayProduct.js
constructor(props) {
super(props);
componentDidMount(){
axios.get(MyGlobleSetting.url + '/api/products')
.then(response => {
})
.catch(function (error) {
console.log(error);
})
tabRow(){
return ;
})
render(){
return (
<div>
<h1>Products</h1>
<div className="row">
<div className="col-md-10"></div>
<div className="col-md-2">
</div>
</div><br />
<thead>
<tr>
<td>ID</td>
<td>Product Title</td>
<td>Product Body</td>
<td width="200px">Actions</td>
</tr>
</thead>
<tbody>
{this.tabRow()}
</tbody>
</table>
</div>
resources/assets/js/components/Master.js
render(){
return (
<div className="container">
<div className="container-fluid">
<div className="navbar-header">
<a className="navbar-brand"
href="https://itsolutionstuff.com">ItSolutionStuff.com</a>
</div>
<li><Link to="/">Home</Link></li>
<li><Link to="display-item">Products</Link></li>
</ul>
</div>
</nav>
<div>
{this.props.children}
</div>
</div>
resources/assets/js/components/MyGlobleSetting.js
class MyGlobleSetting {
constructor() {
this.url = 'http://localhost:8000';
resources/assets/js/components/TableRow.js
import React, { Component } from 'react';
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
handleSubmit(event) {
event.preventDefault();
axios.delete(uri);
browserHistory.push('/display-item');
render() {
return (
<tr>
<td>
{this.props.obj.id}
</td>
<td>
{this.props.obj.title}
</td>
<td>
{this.props.obj.body}
</td>
<td>
<form onSubmit={this.handleSubmit}>
</form>
</td>
</tr>
);
resources/assets/js/components/UpdateProduct.js
constructor(props) {
super(props);
this.handleChange1 = this.handleChange1.bind(this);
this.handleChange2 = this.handleChange2.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
componentDidMount(){
axios.get(MyGlobleSetting.url + `/api/products/${this.props.params.id}/edit`)
.then(response => {
})
.catch(function (error) {
console.log(error);
})
handleChange1(e){
this.setState({
title: e.target.value
})
handleChange2(e){
this.setState({
body: e.target.value
})
handleSubmit(event) {
event.preventDefault();
const products = {
title: this.state.title,
body: this.state.body
this.props.history.push('/display-item');
});
render(){
return (
<div>
<h1>Update Product</h1>
<div className="row">
<div className="col-md-10"></div>
<div className="col-md-2">
</div>
</div>
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label>Product Title</label>
<input type="text"
className="form-control"
value={this.state.title}
onChange={this.handleChange1} />
</div>
<div className="form-group">
<textarea className="form-control"
onChange={this.handleChange2} value={this.state.body}>
</textarea>
</div>
<div className="form-group">
</div>
</form>
</div>
You can see above left step on next link, so click bellow next button, you can also goes on previous link.