PHP Laravel 5.7 insert update delete in MySQL Example

Today, We want to share with you PHP Laravel 5.7 insert update delete in MySQL Example.In this post we will show you insert update delete in php Laravel 5.7, hear for Laravel 5.7 insert update delete in php with source code
we will give you demo and example for implement.In this post, we will learn about how to insert update delete record using php Laravel 5.7 and mysql with an
example.

PHP Laravel 5.7 insert update delete in MySQL Example

There are the Following The simple About PHP Laravel 5.7 insert update delete in MySQL Example Full Information With Example and source
code.

As I will cover this Post with live Working example to develop insert update delete in php Laravel 5.7 with source code download, so the some major files and
Directory structures for this example is following below.

Step 1 : Install Laravel 5.7

Run bellow command Laravel 5.7

composer create-project --prefer-dist laravel/laravel blog

Step 2: Settings Database Configuration

Laravel 5.7 .env files

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=atmiya25
DB_USERNAME=root
DB_PASSWORD=

Step 3: Create Laravel 5.7 Table

Laravel 5.7 php artisan command,

php artisan make:migration create_categorys_table --create=categorys

“database/migrations”

<?php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
  
class CreatecategorysTable extends Migration
{
    public function up()
    {
        Schema::create('categorys', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->text('information');
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categorys');
    }
}

Run this command to create MySQL table

php artisan migrate

Step 4: Create Resource Route

routes/web.php

Route::resource('categorys','categoryController');

Step 5: Make Laravel 5.7 Controller and Model

Create Laravel 5.7 Controller

php artisan make:controller categoryController --resource --model=category

Laravel 5.7 Controllers

app/Http/Controllers/categoryController.php

<?php
  
namespace App\Http\Controllers;
  
use App\category;
use Illuminate\Http\Request;
  
class categoryController extends Controller
{
    public function index()
    {
        $categorys = category::latest()->paginate(5);
  
        return view('categorys.index',compact('categorys'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }
   
    public function create()
    {
        return view('categorys.create');
    }
  
 
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'information' => 'required',
        ]);
  
        category::create($request->all());
   
        return redirect()->route('categorys.index')
                        ->with('success','category created successfully.');
    }
   
    /**
     * Display the specified resource.
     *
     * @param  \App\category  $category
     * @return \Illuminate\Http\Response
     */
    public function show(category $category)
    {
        return view('categorys.show',compact('category'));
    }
   
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\category  $category
     * @return \Illuminate\Http\Response
     */
    public function edit(category $category)
    {
        return view('categorys.edit',compact('category'));
    }
  
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, category $category)
    {
        $request->validate([
            'name' => 'required',
            'information' => 'required',
        ]);
  
        $category->update($request->all());
  
        return redirect()->route('categorys.index')
                        ->with('success','category updated successfully');
    }
  
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\category  $category
     * @return \Illuminate\Http\Response
     */
    public function destroy(category $category)
    {
        $category->delete();
  
        return redirect()->route('categorys.index')
                        ->with('success','category deleted successfully');
    }
}

Laravel 5.7 Model

app/category.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class category extends Model
{
    protected $fillable = [
        'name', 'information'
    ];
}

  • layout.blade.php
  • index.blade.php
  • create.blade.php
  • edit.blade.php
  • show.blade.php
Read Also:  How to Open URL in New Tab using CakePHP

Step 6: Create Laravel 5.7 Blade Files

resources/views/categorys/layout.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 5.7 CRUD Application - pakainfo.com</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>

resources/views/categorys/index.blade.php

@extends('categorys.layout')
 
@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Laravel 5.7 CRUD Example from scratch - pakainfo.com</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('categorys.create') }}"> Create New category</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>No</th>
            <th>categorys Name</th>
            <th>informations</th>
            <th width="280px">Action</th>
        </tr>
        @foreach ($categorys as $category)
        <tr>
            <td>{{ ++$i }}</td>
            <td>{{ $category->name }}</td>
            <td>{{ $category->information }}</td>
            <td>
                <form action="{{ route('categorys.destroy',$category->id) }}" method="POST">
   
                    <a class="btn btn-info" href="{{ route('categorys.show',$category->id) }}">Show</a>
    
                    <a class="btn btn-success" href="{{ route('categorys.edit',$category->id) }}">Edit</a>
   
                    @csrf
                    @method('DELETE')
      
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </table>
  
    {!! $categorys->links() !!}
      
@endsection

resources/views/categorys/create.blade.php

@extends('categorys.layout')
  
@section('content')
<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Add New category</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-success" href="{{ route('categorys.index') }}"> Back</a>
        </div>
    </div>
</div>
   
@if ($errors->any())
    <div class="alert alert-danger">
        <strong>Whoops!</strong> sorry There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
   
<form action="{{ route('categorys.store') }}" method="POST">
    @csrf
  
     <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Name:</strong>
                <input type="text" name="name" class="form-control" placeholder="Name">
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>information:</strong>
                <textarea class="form-control" style="height:150px" name="information" placeholder="information"></textarea>
            </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>
@endsection

resources/views/categorys/edit.blade.php

@extends('categorys.layout')
   
@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Edit category</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('categorys.index') }}"> Back</a>
            </div>
        </div>
    </div>
   
    @if ($errors->any())
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems(for category) with your input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
  
    <form action="{{ route('categorys.update',$category->id) }}" method="POST">
        @csrf
        @method('PUT')
   
         <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>category Name:</strong>
                    <input type="text" name="name" value="{{ $category->name }}" class="form-control" placeholder="category Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>information:</strong>
                    <textarea class="form-control" style="height:150px" name="information" placeholder="information">{{ $category->information }}</textarea>
                </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>
@endsection

resources/views/categorys/show.blade.php

@extends('categorys.layout')
@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2> Show category</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('categorys.index') }}"> Back</a>
            </div>
        </div>
    </div>
   
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Name:</strong>
                {{ $category->name }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>informations:</strong>
                {{ $category->information }}
            </div>
        </div>
    </div>
@endsection

Laravel 5.7 Save and quick run

php artisan serve

Run Laravel 5.7 project

http://localhost:8000/categorys

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  Send HTML Form Data as JSON via Ajax

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Laravel 5.7 insert update delete in MySQL Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Update multiple records using Laravel
  2. Read Also:  React JSX Basics Step By Step
  3. Read Also:  How to get the browser viewport height and width dimensions?
  4. Read Also:  how to split a string in python? | python split string into list
  5. Read Also:  difference between php and html
  6. Read Also:  Laravel Eloquent JOIN Multiple Tables using Query Builder
  7. Read Also:  C#.Net OOPS Concepts, Features & Explanation
  8. Read Also:  Dynamically Upload Multiple Images Using PHP and jQuery
  9. Read Also:  How to Open URL in New Tab using CakePHP
  10. Read Also:  Send HTML Form Data as JSON via Ajax
CLOSEX