Laravel INSERT UPDATE DELETE Example Step By Step

Laravel INSERT UPDATE DELETE Example Step By Step

In this Post We Will Explain About is Laravel INSERT UPDATE DELETE Example – CRUD Operations in Laravel 5 With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Laravel insert update delete example in phpExample

In this post we will show you Best way to implement insert update delete in Laravel Example, hear for Laravel crud application demo,with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

You can download source code and Demo from below link.




CRUD Operations in Laravel 5

There are the following List of step By step Create file and folder

  • Database Table Creation
  • Model Creation (page)
  • Controller Creation (pages)
  • Routing
  • Layouts and
  • Views

Database Table Creation

First of all simple created this folder or directory

cd /var/www/html/laravel

second step to simple create a Laravel Project to run this command

php artisan make:migration create_posts_table --create=article

Open the your created : database/migrations/xxxxxx_create_posts_table.php file and simple add some Lines and structure the fields to store level the basic information Like as a:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{

    public function up()
    {
        Schema::create('article', function (Blueprint $table) {
            $table->increments('id');
            $table->string('header', 100);
            $table->text('datadesc');
            $table->dateTime('cr_date');
            $table->dateTime('modified');
        });
    }

    public function down()
    {
        Schema::drop('article');
    }
}

run this command to create your table with database

php artisan migrate




Model Creation (page)

php artisan make:model page

The newly simple cr_date fields page model will be simple located in Like as a “app/” directory.

app/page.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class page extends Model
{
    //simple fillable fields
    protected $fillable = ['header', 'datadesc'];
    
    //simple custom timestamps name
    const CREATED_AT = 'cr_date';
    const UPDATED_AT = 'modified';
}

Controller Creation (Posts)

app/Http/Controllers

define a myliveController by creating a simple “app/Http/Controllers/myliveController.php” file.

1.index() Method
2.details() Method
3.add() Method
4.insert() Method
5.edit() Method
6.update() Method
7.delete()

myliveController.php

<?php
namespace App\Http\Controllers;
use App\page;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Session;

class myliveController extends Controller
{
    
    public function index(){
        //fetch simple all article data
        $article = page::orderBy('cr_date','desc')->get();
        
        //pass article data to view and load simple list view
        return view('article.index', ['article' => $article]);
    }
    
    public function details($id){
        //fetch page data
        $page = page::find($id);
        
        //pass article data to view and load list view
        return view('article.details', ['page' => $page]);
    }
    
    public function add(){
        //load form view
        return view('article.add');
    }
    
    public function insert(Request $request){
        //validate page data
        $this->validate($request, [
            'header' => 'required',
            'datadesc' => 'required'
        ]);
        
        //get page data
        $postData = $request->all();
        
        //insert page data
        page::create($postData);
        
        //store status message
        Session::flash('success_msg', 'page added successfully!');

        return redirect()->route('article.index');
    }
    
    public function edit($id){
        //get page data by id
        $page = page::find($id);
        
        //load form view
        return view('article.edit', ['page' => $page]);
    }
    
    public function update($id, Request $request){
        //validate page data
        $this->validate($request, [
            'header' => 'required',
            'datadesc' => 'required'
        ]);
        
        //get page data
        $postData = $request->all();
        
        //update page data
        page::find($id)->update($postData);
        
        //store status message
        Session::flash('success_msg', 'page updated successfully!');

        return redirect()->route('article.index');
    }
    
    public function delete($id){
        //update page data
        page::find($id)->delete();
        
        //store status message
        Session::flash('success_msg', 'page deleted successfully!');

        return redirect()->route('article.index');
    }
    
}




Routing

app/Http/routes.php

<?php

use App\page;
use Illuminate\Http\Request;

Route::get('/article', 'myliveController@index')->name('article.index');
Route::get('/article/details/{id}', 'myliveController@details')->name('article.details');
Route::get('/article/add', 'myliveController@add')->name('article.add');
Route::page('/article/insert', 'myliveController@insert')->name('article.insert');
Route::get('/article/edit/{id}', 'myliveController@edit')->name('article.edit');
Route::page('/article/update/{id}', 'myliveController@update')->name('article.update');
Route::get('/article/delete/{id}', 'myliveController@delete')->name('article.delete');

Layouts

Create a simple resources/views/layouts/app.blade.php file.

app.blade.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <header>Simple Laravel CRUD Operations - Step by step</header>
         <!-- External Libs part start -->
        <!-- bootstrap minified css using Live24u.com -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <!-- jQuery library using Live24u.com-->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <!-- bootstrap minified js using Live24u.com-->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <!-- External Libs part End -->
        <!-- custom CSS using Live24u.com-->
        <style>
        h1{font-size:25px;}
        .pull-left h2{margin-top:0;font-size:30px;}
        </style>
    </head>

    <body>
        <div class="container">
            <h1>CRUD simple (Create Read Update Delete) new Operations in Laravel</h1>
            @yield('datadesc')
        </div>
    </body>
</html>




Views

resources/views/article

index.blade.php:

@extends('layouts.app')

@section('datadesc')
<div class="row">
    <div class="col-lg-12">
        @if(Session::has('success_msg'))
        <div class="alert alert-success">{{ Session::get('success_msg') }}</div>
        @endif
    <!-- Posts list -->
    @if(!empty($article))
        <div class="row">
            <div class="col-lg-12 margin-tb">
                <div class="pull-left">
                    <h2>Posts List </h2>
                </div>
                <div class="pull-right">
                    <a class="btn btn-success" href="{{ route('article.add') }}"> Add New</a>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <table class="table table-striped task-table">
                    <!-- Table Headings -->
                    <thead>
                        <th>header</th>
                        <th>comments</th>
                        <th>Created</th>
                        <th>Action</th>
                    </thead>
    
                    <!-- simple Table Body -->
                    <tbody>
                    @foreach($article as $page)
                        <tr>
                            <td class="table-text">
                                <div>{{$page->header}}</div>
                            </td>
                            <td class="table-text">
                                <div>{{$page->datadesc}}</div>
                            </td>
                                <td class="table-text">
                                <div>{{$page->cr_date}}</div>
                            </td>
                            <td>
                                <a href="{{ route('article.details', $page->id) }}" class="label label-success">Details</a>
                                <a href="{{ route('article.edit', $page->id) }}" class="label label-warning">Edit</a>
                                <a href="{{ route('article.delete', $page->id) }}" class="label label-danger" onclick="return confirm('Are you sure to delete records?')">Delete</a>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    @endif
    </div>
</div>
@endsection




details.blade.php:

@extends('layouts.app')

@section('datadesc')
<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Read page</h2>
        </div>
        <div class="pull-right">
            <a href="{{ route('article.index') }}" class="label label-primary pull-right"> Back</a>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
            <strong>header:</strong>
            {{ $page->header }}
        </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
            <strong>comments:</strong>
            {{ $page->datadesc }}
        </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
            <strong>Published On:</strong>
            {{ $page->cr_date }}
        </div>
    </div>
</div>
@endsection




add.blade.php:

@extends('layouts.app')

@section('datadesc')
<div class="row">
    <div class="col-lg-12">
        @if($errors->any())
            <div class="alert alert-danger">
            @foreach($errors->all() as $error)
                <p>{{ $error }}</p>
            @endforeach()
            </div>
        @endif
        <div class="panel panel-default">
            <div class="panel-heading">
                Add a New page <a href="{{ route('article.index') }}" class="label label-primary pull-right">Back</a>
            </div>
            <div class="panel-body">
                <form action="{{ route('article.insert') }}" method="POST" class="form-horizontal">
                    {{ csrf_field() }}
                    <div class="form-group">
                        <label class="control-label col-sm-2" >header</label>
                        <div class="col-sm-10">
                            <input type="text" name="header" id="header" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2" >comments</label>
                        <div class="col-sm-10">
                            <textarea name="datadesc" id="datadesc" class="form-control"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <input type="submit" class="btn btn-default" value="Add page" />
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection




edit.blade.php:

@extends('layouts.app')

@section('datadesc')
<div class="row">
    <div class="col-lg-12">
        @if($errors->any())
            <div class="alert alert-danger">
            @foreach($errors->all() as $error)
                <p>{{ $error }}</p>
            @endforeach()
            </div>
        @endif
        <div class="panel panel-default">
            <div class="panel-heading">
                Edit page <a href="{{ route('article.index') }}" class="label label-primary pull-right">Back</a>
            </div>
            <div class="panel-body">
                <form action="{{ route('article.update', $page->id) }}" method="POST" class="form-horizontal">
                    {{ csrf_field() }}
                    <div class="form-group">
                        <label class="control-label col-sm-2" >header</label>
                        <div class="col-sm-10">
                            <input type="text" name="header" id="header" class="form-control" value="{{ $page->header }}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2" >comments</label>
                        <div class="col-sm-10">
                            <textarea name="datadesc" id="datadesc" class="form-control">{{ $page->datadesc }}</textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <input type="submit" class="btn btn-default" value="Update page" />
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

Simple run your Laravel Project URL

http://example.com/article

You can download source code and Demo from below link.





Example

I hope you have Got What is CRUD (Create Read Update Delete) Example in Laravel 5.2 And how it works.I would Like to have FeadBack From My Blog(live24u.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(live24u.com) Are Most Always Welcome.

http://live24u.com/wp-content/uploads/2017/06/Laravel-INSERT-UPDATE-DELETE-Example-CRUD-Operations-in-Laravel-5-1024x576.jpghttp://live24u.com/wp-content/uploads/2017/06/Laravel-INSERT-UPDATE-DELETE-Example-CRUD-Operations-in-Laravel-5-150x150.jpgadminAjaxAPICSSHtmlJavaScriptjQueryJSONLaravelMysqlMysqliPHPTop 10CRUD (Create Read Update Delete) Example in Laravel 5.2,crud operation in mvc 4 using Laravel,insert update delete in Laravel Example,Laravel add edit delete,Laravel crud application demo,Laravel insert data to database,Laravel insert update delete example,Laravel insert update delete example in phpLaravel INSERT UPDATE DELETE Example Step By Step In this Post We Will Explain About is Laravel INSERT UPDATE DELETE Example - CRUD Operations in Laravel 5 With Example and Demo.Welcome on Live24u.com - Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can...BIGGEST AND FASTEST Web Tutorials - live24u.com

No Comments Yet

Leave a Reply

Your email address will not be published.