Laravel – INSERT, UPDATE, DELETE – CRUD

Laravel – INSERT, UPDATE, DELETE – CRUD

In this Post We Will Explain About is Laravel – INSERT, UPDATE, DELETE – CRUD 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 with Eloquent ORM Example

In this post we will show you Best way to implement CRUD (Create Read Update Delete) in a Laravel App, hear for Laravel 5.2 CRUD (Create Read Update Delete) Example from Scratch with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




Step 1: Laravel Installation

First of all simple create your project

composer create-project --prefer-dist laravel/laravel <your Project Name> "5.2.*"

Step 2: Create students table and model

php artisan make:migration create_students_table

Simple Step by step run this command We will search one file in following simple folder path Like as a database/migrations and We have to put bellow source code in your migration data file for create any students table.

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateItemsTable extends Migration
{
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('description');
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::drop("students");
    }
}

run this command to genrate table

php artisan migrate

Now craete “students” table you should simple mysql craete here Student model for students , so Second make file in this path Like as a app/Student.php and add bellow some source content in student.php file:

app/Student.php

namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
    public $fillable = ['title','description'];
}

Step 3: Put Route and Controller

After that We have to put route for students CRUD operation, in this Simple Example We put resource route for web-application, if we add resource route then it will add Like as a index file, create file, show file, as well as edit file and delete file route simple automatically. Bcoz add bellow source code in your route file.

Also Read :   Restful API insert update delete using angularjs and php

Route::get('StudentCRUD',['as' => 'studentCRUD.index', 'uses' => 'StudentCRUDController@index']);
Route::get('StudentCRUD/create',['as' => 'studentCRUD.create', 'uses' => 'StudentCRUDController@create']);
Route::post('StudentCRUD/store',['as' => 'studentCRUD.store', 'uses' => 'StudentCRUDController@store']);
Route::get('StudentCRUD/show/{id}',['as' => 'studentCRUD.show', 'uses' => 'StudentCRUDController@show']);
Route::get('StudentCRUD/{id}/edit',['as' => 'studentCRUD.edit', 'uses' => 'StudentCRUDController@edit']);
Route::patch('StudentCRUD/update/{id}',['as' => 'studentCRUD.update', 'uses' => 'StudentCRUDController@update']);
Route::delete('StudentCRUD/destroy/{id}',['as' => 'studentCRUD.destroy', 'uses' => 'StudentCRUDController@destroy']);

And Onther steps, we should make new Laravel controller as StudentCRUDController in this path Like as a app/Http/Controllers/StudentCRUDController.php. this Laravel controller will manage all files route method:

app/Http/Controllers/StudentCRUDController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Student;
class StudentCRUDController extends Controller
{

    public function index(Request $request)
    {
        $students = Student::orderBy('id','DESC')->paginate(5);
        return view('StudentCRUD.index',compact('students'))
            ->with('i', ($request->input('page', 1) - 1) * 5);

    }

    public function create()
    {
        return view('StudentCRUD.create');
    }
    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',
            'description' => 'required',
        ]);
        Student::create($request->all());
        return redirect()->route('studentCRUD.index')
                        ->with('success','Student created successfully');

    }
    public function show($id)
    {
        $student = Student::find($id);
        return view('StudentCRUD.show',compact('student'));
    }

    public function edit($id)
    {
        $student = Student::find($id);
        return view('StudentCRUD.edit',compact('student'));
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' => 'required',
            'description' => 'required',
        ]);
        Student::find($id)->update($request->all());
        return redirect()->route('studentCRUD.index')
                        ->with('success','Student updated successfully');
    }

    public function destroy($id)
    {
        Student::find($id)->delete();
        return redirect()->route('studentCRUD.index')
                        ->with('success','Student deleted successfully');
    }
}

Step 4: Create Blade File

And, we will make blade file for student listing, student craete, student edit and show student and also make default blade file for theme setting. therefore first we make new layouts directory and make Like as a default.blade.php file simple inside that view folder.

resources/views/layouts/default.blade.php

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel Simple Insert Update Delete - CRUD</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">

</head>
<body>
<div class="container">
    @yield('content')
</div>
</body>

</html>

Ok, now we will make layout for students listing, So first we make new StudentCRUD directory and create index.blade.php file as well as inside that folder.

resources/views/StudentCRUD/index.blade.php

@extends('layouts.default')
@section('content')
    <div class="live24u row">
        <div class="live24u col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Students CRUD Example</h2>
            </div>
            <div class="live24u pull-right">
                <a class="btn btn-success" href="{{ route('studentCRUD.create') }}"> Create New Student</a>
            </div>
        </div>
    </div>
    @if ($message = Session::get('success'))
        <div class="alert alert-success">
            <p>{{ $message }}</p>
        </div>
    @endif
    <table class="table table-bordered">
        <tr>
            <th>Number</th>
            <th>Title</th>
            <th>Description</th>
            <th width="290px">Action</th>
        </tr>
    @foreach ($students as $key => $student)
    <tr>
        <td>{{ ++$i }}</td>
        <td>{{ $student->title }}</td>
        <td>{{ $student->description }}</td>
        <td>
            <a class="btn btn-info" href="{{ route('studentCRUD.show',$student->id) }}">Show</a>

            <a class="btn btn-success" href="{{ route('studentCRUD.edit',$student->id) }}">Edit</a>

            {!! Form::open(['method' => 'DELETE','route' => ['studentCRUD.destroy', $student->id],'style'=>'display:inline']) !!}

            {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}

            {!! Form::close() !!}
        </td>
    </tr>
    @endforeach
    </table>
    {!! $students->render() !!}
@endsection

Now we make new blade file for make new student, it’s call create.blade.php file inside “StudentCRUD”.

Also Read :   Files Uploading on Amazon S3 server using Laravel 5.4 Part-1

resources/views/StudentCRUD/create.blade.php

@extends('layouts.default')
@section('content')

    <div class="live24u row">
        <div class="live24u col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Create New Student</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('studentCRUD.index') }}"> Back</a>
            </div>
        </div>
    </div>

    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your Data input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)

                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>

    @endif

    {!! Form::open(array('route' => 'studentCRUD.store','method'=>'POST')) !!}
    <div class="live24u row">
        <div class="live24u col-xs-12 col-sm-12 col-md-12">
            <div class="live24u form-group">

                <strong>Title:</strong>

                {!! Form::text('title', null, array('placeholder' => 'Title','class' => 'form-control')) !!}
            </div>
        </div>

        <div class="live24u col-xs-12 col-sm-12 col-md-12">
            <div class="live24u form-group">

                <strong>Description:</strong>

                {!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px')) !!}

            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-success">Submit</button>
        </div>
    </div>

    {!! Form::close() !!}

@endsection

And then, we have to make show.blade.php file for student details page, so again make this file in StudentCRUD folder.

resources/views/StudentCRUD/show.blade.php

@extends('layouts.default')

@section('content')

    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">

                <h2> Show Student</h2>

            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('studentCRUD.index') }}"> Back</a>
            </div>
        </div>
    </div>
    <div class="live24u row">
        <div class="live24u col-xs-12 col-sm-12 col-md-12">

            <div class="form-group">

                <strong>Title:</strong>

                {{ $student->title }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">

            <div class="form-group">

                <strong>Description:</strong>

                {{ $student->description }}
            </div>
        </div>
    </div>
@endsection

Simple And Last Steps, we need to create a simple edit.blade.php file for for update all the student, so again here create this simple laravel file in StudentCRUD folder.

resources/views/StudentCRUD/edit.blade.php

@extends('layouts.default')

@section('content')
    <div class="live24u row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Edit New Student</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('studentCRUD.index') }}"> Back</a>
            </div>
        </div>
    </div>
    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some Laravel problems with your input Box.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>

                @endforeach
            </ul>
        </div>
    @endif
    {!! Form::model($student, ['method' => 'PATCH','route' => ['studentCRUD.update', $student->id]]) !!}
    <div class="live24u row">
        <div class="live24u col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Title:</strong>
                {!! Form::text('title', null, array('placeholder' => 'Title','class' => 'form-control')) !!}
            </div>
        </div>
        <div class="live24u col-xs-12 col-sm-12 col-md-12">
            <div class="live24u form-group">
                <strong>Description:</strong>
                {!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px')) !!}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-success">Submit</button>
        </div>
    </div>
    {!! Form::close() !!}
@endsection

Error Solution : HTML/FORM not found in Laravel 5.2

Laravel For 5.2 version Error solution:

Also Read :   Insert update delete Using Laravel 5.2 Ajax CRUD laravel without refresh page

We are going to the Laravel simple data Collective site and installation processes have step by step demonstrated their.

Simple 5.2 command line Execute the command

composer require "laravelcollective/html":"^5.2.0"

After that, in first options Like as a provider array display which is in here path config/app.php.here put this simple line at last with comma(,)

//provider array
Collective\Html\HtmlServiceProvider::class,

For use Simple HTML and FORM text we some need to all the here aliases them in aliases array of like as a config/app.php. put the two simple line at the last line.

//aliases array
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,

Useful Laravel Commands

php artisan make:migration create_products_table

create simple migration

php artisan migrate

php artisan migrate:rollback

php artisan make:controller ProductCRUDController

php artisan route:clear

php artisan cache:clear

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example





Example

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