Today, We want to share with you PHP Laravel MySQL CRUD Application.In this post we will show you CRUD Operations in Laravel PHP Framework, hear for Laravel 5.7 CRUD Operations Tutorial step by step we will give you demo and example for implement.In this post, we will learn about CRUD (Create Read Update Delete) in a Laravel 5.7 App with an example.
PHP Laravel MySQL CRUD Application
There are the Following The simple About PHP Laravel MySQL CRUD Application Full Information With Example and source code.
As I will cover this Post with live Working example to develop simple crud application in laravel 5.7, so the
Laravel 5.7 CRUD Tutorial With Example from scratch
for this example is following below.
In this Laravel 5.7 Insert Update Delete Examples, I am creating CRUD for products table. After all these steps, your output will look like this.
Create Laravel Project:
First, we need to create first of all simple crud application in laravel 5 by runing following command in your terminal.
composer create-project --prefer-dist laravel/laravel laravel_crud
Configure .env file:
After creating a laravel project, open your project in your any editor and open .env file and configure your database setting in .env change following value in that file like that
.env change
DB_DATABASE=databasename DB_USERNAME=root DB_PASSWORD=Abcd@!#7854
Make Laravel Auth: laravel crud generator
Next, I am create laravel bydefault authentication by run following command. laravel 5.7all the CRUD Auth provide ready made authentication system so I am use that.
php artisan make:auth
Create Route:
Now, open your first of all main nav call routes/web.php file and add one route using recource in this file for our products crud. here i am use resource instade of all changes saperate route.
routes/web.php
Route::resource('products', 'productsController');
Simple add above line in your web.php file with Onther Examples with step by step PHP Laravel 5.7 insert update delete in MySQL Example
Change in app.blade.php file:
Next, I am change some path in resources/views/layouts/app.blade.php file so, open this file and laravel 5.7 crud step by step copy this all the source code as well as past in your app.blade.php file.
resources/views/layouts/app.blade.php
{{ config('app.name', 'Laravel') }} @include('productDeleteModel')@yield('content')@include('alert')
Create productDeleteModel.blade.php file:
After, create productDeleteModel.blade.php file for pure soft delete CRUD Product delete confurm model on this path resources/views
productDeleteModel.blade.php
Create alert.blade.php file:
And then, create alert.blade.php file in this path resources/views I am create this file for all notification alert like when I am insert data and display success message and failed message or any warning message.
resources/views/alert.blade.php
@if ($errors->any())Error Alert! Please check the form below for errors@endif @if ($message = Session::get('success'))Success Alert!@endif @if ($message = Session::get('error'))Error Alert!@endif @if ($message = Session::get('warning'))Waarning Alert!@endif @if ($message = Session::get('info'))Information Alert!@endif
Create custom.js File:
Now, I am create custom.js file on this path public/js and in this file I am write js source code for out delete confirm model. this js main logic We can use any your laravel 5.7 project or application for delete records with confirmation model.
public/js/custom.js
$(document).ready(function(){ // For A Delete product Record Popup $('.remove-record').click(function() { var id = $(this).attr('data-id'); var url = $(this).attr('data-url'); var token = CSRF_TOKEN; $(".product-model-remove-record").attr("action",url); $('body').find('.product-model-remove-record').append(''); $('body').find('.product-model-remove-record').append(''); $('body').find('.product-model-remove-record').append(''); }); $('.remove-data-from-delete-form').click(function() { $('body').find('.product-model-remove-record').find( "input" ).remove(); }); $('.modal').click(function() { // $('body').find('.product-model-remove-record').find( "input" ).remove(); }); });
Create products Table Migration:
Now, I am create products table migration by run following below command in CMD on terminal.
php artisan make:migration create_products_tbl
After runing this command in your CMD(terminal) then one mingration file automatic generated in your this path Like database/migrations/ then here your diplay one new migration file therefor, open it as well as change like thai in this file.
database/migrations/
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateproductsTbl extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->string('title')->nullble(); $table->text('products')->nullble(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('products'); } }
After change in this Database migration file then run this migration by following simple command in your CMD terminal.
php artisan migrate
Create products Table Model:
And then, create products table model in following path Like as a app/Product.php and add following source code in this file.
namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { protected $table = 'products'; protected $guarded = array(); public function getProductData() { return static::orderBy('id', 'desc')->paginate(5); } public function AddProductData($input) { return static::create($input); } public function findProductData($id) { return static::find($id); } public function updateProductData($id, $input) { return static::where('id', $id)->update($input); } public function destroyProductData($id) { return static::where('id',$id)->delete(); } }
Create productsController:
Now, create productsController.php file in this path Like app/Http/Controllers and simple data copy this following source code in this file.
app/Http/Controllers/productsController.php
namespace App\Http\Controllers; use App\Http\Requests; use Illuminate\Http\Request; use App\Product; class productsController extends HomeController { public function __construct() { parent::__construct(); $this->Product = new Product; $this->moduleTitleS = 'products'; $this->dirModuleName = 'products'; view()->share('dirModuleName',$this->dirModuleName); view()->share('moduleTitleS',$this->moduleTitleS); } public function index() { $data = $this->Product->getProductData(); return view($this->dirModuleName.'.index',compact('data')) ->with('i',0); } public function create() { return view($this->dirModuleName.'.create'); } public function store(Request $request) { $this->validate($request, [ 'title' => 'required|max:255', 'products' => 'required', ]); $input = array_except($request->all(),array('_token')); $this->Product->AddProductData($input); \Session::put('success','Product Store Successfully!!'); return redirect()->route('products.index'); } public function edit($id) { $data = $this->Product->findProductData($id); return view($this->dirModuleName.'.edit',compact('data')); } public function update(Request $request, $id) { $this->validate($request, [ 'title' => 'required|max:255', 'products' => 'required', ]); $input = array_except($request->all(),array('_token', '_method')); $this->Product->updateProductData($id, $input); \Session::put('success','Product Updated Successfully!!'); return redirect()->route('products.index'); } public function destroy($id) { $this->Product->destroyProductData($id); \Session::put('success','Product Delete Successfully!!'); return redirect()->route('products.index'); } }
Create All View Blade File:
Now, I am create all following Laravel 5.7 blade file one by one
- 1.)index.blade.php
- 2.)create.blade.php
- 3.)edit.blade.php
resources/views/products/index.blade.php File:
Let’s Now, make this file and step by step copy and path following source code in this file for all products listing.
@extends('layouts.app') @section('content')@endsection{!! $data->appends([])->render() !!}All products Listing
@if(!empty($data) && $data->count()) @foreach($data as $key => $value) Title products Description Action @endforeach @endif {{ $value->title }} @if (strlen($value->products) > 100) {!! substr(strip_tags(html_entity_decode($value->products)), 0, 100) . '.....' !!} @else {!! strip_tags($value->products) !!} @endif Edit Delete
resources/views/products/create.blade.php File:
After that, create this file for create and insert products records in our database. step by step you all thw source copy this all following code and path in your this file.
@extends('layouts.app') @section('content')@endsectionCreate Product
resources/views/products/edit.blade.php
And then, I am create view Blade File edit file for update any data record in our Laravel MySQL database. this Source code like that
Edit Product{{ Form::model($data, ['route' => ['products.update', $data->id], 'method' => 'patch', 'class' =>'form-horizontal']) }}@if ($errors->has('title')) {{ $errors->first('title') }} @endif@if ($errors->has('products')) {{ $errors->first('products') }} @endif{{ Form::close() }}