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 infinityknow.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!.

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:

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.

READ :  Laravel Routing GET and POST Route parameters controller

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

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

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

Layouts

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

app.blade.php


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

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

Views

resources/views/article

READ :  Laravel Set and Get Global variables using controller

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>id) }}" class="label label-success">Details</a>
                                <a>id) }}" class="label label-warning">Edit</a>
                                <a>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">
                            
                        </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">
                            
                        </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>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">
                            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">
                            
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

Simple run your Laravel Project URL

READ :  VueJs Image Carousels Sliders with Demo

http://example.com/article

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(infinityknow.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(infinityknow.com) Are Most Always Welcome.

https://pakainfo.com/

Add a Comment

Your email address will not be published. Required fields are marked *