Laravel 5 Simple Crud Application Using Reactjs Part 1
Laravel 5 Simple Crud Application Using Reactjs Part 1
Laravel 5 Simple Crud Application Using Reactjs Part 1
com
Hardik Savani
6-8 minutes
By Hardik Savani | December 10, 2017 | Category : PHP Laravel Javascript Bootstrap jQuery MySql JSON
Node JS Ajax React JS Axios
In this post, i want to share with you how to create crud(Create Read Update Delete) application with react js in
PHP Laravel framework. In this example you can learn how to built setup for laravel reactjs application, I also
used axios post request, get request, put request and delete request for insert update delete application.
we always would like to prefer make setup for any javascript framework like vuejs, angularjs, reactjs etc. here i
share with you basic setup for react and laravel with crud application. You have to just follow following 9 step as
listed bellow:
After successfully all step you will get crud application like as bellow screen shot i attached.
Preview:
we are going from scratch, So we require to get fresh Laravel 5.5 version application using bellow command,
So open your terminal OR command prompt and run bellow command:
In second step, we should make database configuration for example database name, username, password etc
for our crud application of laravel 5.5. So let's open .env file and fill all details like as bellow:
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
we are going to create crud application for products table with react js. so we have to create migration for
products table using Laravel 5.5 php artisan command, so first fire bellow command:
After this command you will find one file in following path database/migrations and you have to put bellow
code in your migration file for create products table.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
/**
* @return void
*/
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
/**
* @return void
*/
Schema::dropIfExists('products');
}
After creating table we have to create model for "products" table so just run bellow command and create new
model:
Ok, so after run bellow command you will find app/Product.php and put bellow content in Product.php file:
app/Product.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* @var array
*/
protected $fillable = [
'title', 'body'
];
In this is step we need to add web route and also api route for products. so open your routes/web.php file and
add following route.
routes/web.php
Route::get('/', function () {
return view('welcome');
});
routes/api.php
Route::resource('products', 'ProductController');
In this step, we need create new controller as ProductController. So run bellow command and create new
controller. bellow controller for create resource controller.
After bellow command you will find new file in this path app/Http/Controllers/ProductController.php.
1)index()
2)store()
3)edit()
4)update()
5)destroy()
app/Http/Controllers/ProductController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
/**
* @return \Illuminate\Http\Response
*/
$products = Product::all();
return response()->json($products);
/**
* @return \Illuminate\Http\Response
*/
]);
$product->save();
/**
* @return \Illuminate\Http\Response
*/
$product = Product::find($id);
return response()->json($product);
/**
* @return \Illuminate\Http\Response
*/
$product = Product::find($id);
$product->title = $request->get('title');
$product->body = $request->get('body');
$product->save();
/**
* @return \Illuminate\Http\Response
*/
$product = Product::find($id);
$product->delete();
You can see above left step on next link, so click bellow next button.