Laravel Validate Insert Update Delete example
Step 1: Install Laravel 8
step: Install UI
First, install a new Laravel app just running the below command in your terminal.
composer create-project --prefer-dist laravel/laravel laravel_basic
Step 2: Update Your Database Credentials
After that update your database credentials in your .env file which is located in your project root.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_db
DB_USERNAME=root
DB_PASSWORD=#your database password
Step 3: Make Migration
After adding the migration file now run the migrate command.
php artisan migrate
Step 4: Add in Home Page
Add the below code in your home.blade.php file.
Resources/Views/home.blade.php
@extends('layouts.master')
@section('content')
<div class="signup-form">
<form action="{{ route('form/page_test/save') }}" method="post" class="form-horizontal">
{{ csrf_field() }}
<div class="row">
<div class="col-8 offset-4">
<h2>Personal</h2>
</div>
</div>
{{-- success --}}
@if(\Session::has('insert'))
<div id="insert" class=" alert alert-success">
{!! \Session::get('insert') !!}
</div>
@endif
{{-- error --}}
@if(\Session::has('error'))
<div id="error" class=" alert alert-danger">
{!! \Session::get('error') !!}
</div>
@endif
<div class="form-group row">
<label class="col-form-label col-4">Full Name</label>
<div class="col-8">
<input type="text" class="form-control @error('username') is-invalid @enderror" name="username" placeholder="Enter UserName">
@error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-4">Email Address</label>
<div class="col-8">
<input type="email" class="form-control @error('email') is-invalid @enderror" name="email" placeholder="Enter Email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-4">Phone Number</label>
<div class="col-8">
<input type="tel" class="form-control @error('phone') is-invalid @enderror" name="phone" placeholder="Enter Phone number">
@error('phone')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<div class="col-8 offset-4">
<button type="submit" class="btn btn-primary btn-lg">Save</button>
</div>
</div>
</form>
</div>
{{-- <table> --}}
<div class="container">
<div class="container-fluid">
<table id="example" class="table table-striped table-bordered nowrap" style="width:100%">
<thead>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Email</th>
<th>Phone Number</th>
<th>Modefy</th>
</tr>
</thead>
<tbody>
@foreach($data as $value)
<tr>
<td class="id">{{ $value->id }}</td>
<td class="name">{{ $value->username }}</td>
<td class="email">{{ $value->email }}</td>
<td class="phone">{{ $value->phone }}</td>
<td class=" text-center">
<a class="m-r-15 text-muted update" data-toggle="modal" data-id="'.$value->id.'" data-target="#update">
<i class="fa fa-edit" style="color: #2196f3"></i>
</a>
<a href="{{ url('form/delete'.$value->id) }}" onclick="return confirm('Are you sure to want to delete it?')">
<i class="fa fa-trash" style="color: red;"></i>
</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
{{-- </table> --}}
<!-- Modal Update-->
<div class="modal fade" id="update" tabindex="-1" aria-labelledby="update" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="update">Update</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form id="update" action="{{ route('form/update') }}" method = "post"><!-- form add -->
{{ csrf_field() }}
<div class="modal-body">
<input type="hidden" class="form-control" id="e_id" name="id" value=""/>
<div class="modal-body">
<div class="form-group row">
<label for="" class="col-sm-3 col-form-label">Full Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="e_name" name="name" required="" value=""/>
</div>
</div>
<div class="form-group row">
<label for="" class="col-sm-3 col-form-label">Email</label>
<div class="col-sm-9">
<input type="email" class="form-control" id="e_email" name="email" required="" value=""/>
</div>
</div>
<div class="form-group row">
<label for="" class="col-sm-3 col-form-label">Phone</label>
<div class="col-sm-9">
<input type="tel" class="form-control" id="e_phone" name="phone" required="" value=""/>
</div>
</div>
</div>
<!-- form add end -->
</div>
<div class="modal-footer">
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal"><i class="icofont icofont-eye-alt"></i>Close</button>
<button type="submit" id=""name="" class="btn btn-success waves-light"><i class="icofont icofont-check-circled"></i>Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
<!-- End Modal Update-->
{{-- script update --}}
<script>
$(document).on('click','.update',function()
{
var _this = $(this).parents('tr');
$('#e_id').val(_this.find('.id').text());
$('#e_name').val(_this.find('.name').text());
$('#e_email').val(_this.find('.email').text());
$('#e_phone').val(_this.find('.phone').text());
});
</script>
{{-- hide message js --}}
<script>
$('#insert').show();
setTimeout(function()
{
$('#insert').hide();
},5000);
$('#error').show();
setTimeout(function()
{
$('#error').hide();
},5000);
</script>
@endsection
@extends('layouts.master')
@section('content')
<div class="signup-form">
<form action="{{ route('form/page_test/save') }}" method="post" class="form-horizontal">
{{ csrf_field() }}
<div class="row">
<div class="col-8 offset-4">
<h2>Personal</h2>
</div>
</div>
{{-- success --}}
@if(\Session::has('insert'))
<div id="insert" class=" alert alert-success">
{!! \Session::get('insert') !!}
</div>
@endif
{{-- error --}}
@if(\Session::has('error'))
<div id="error" class=" alert alert-danger">
{!! \Session::get('error') !!}
</div>
@endif
<div class="form-group row">
<label class="col-form-label col-4">Full Name</label>
<div class="col-8">
<input type="text" class="form-control @error('username') is-invalid @enderror" name="username" placeholder="Enter UserName">
@error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-4">Email Address</label>
<div class="col-8">
<input type="email" class="form-control @error('email') is-invalid @enderror" name="email" placeholder="Enter Email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-4">Phone Number</label>
<div class="col-8">
<input type="tel" class="form-control @error('phone') is-invalid @enderror" name="phone" placeholder="Enter Phone number">
@error('phone')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<div class="col-8 offset-4">
<button type="submit" class="btn btn-primary btn-lg">Save</button>
</div>
</div>
</form>
</div>
{{-- <table> --}}
<div class="container">
<div class="container-fluid">
<table id="example" class="table table-striped table-bordered nowrap" style="width:100%">
<thead>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Email</th>
<th>Phone Number</th>
<th>Modefy</th>
</tr>
</thead>
<tbody>
@foreach($data as $value)
<tr>
<td class="id">{{ $value->id }}</td>
<td class="name">{{ $value->username }}</td>
<td class="email">{{ $value->email }}</td>
<td class="phone">{{ $value->phone }}</td>
<td class=" text-center">
<a class="m-r-15 text-muted update" data-toggle="modal" data-id="'.$value->id.'" data-target="#update">
<i class="fa fa-edit" style="color: #2196f3"></i>
</a>
<a href="{{ url('form/delete'.$value->id) }}" onclick="return confirm('Are you sure to want to delete it?')">
<i class="fa fa-trash" style="color: red;"></i>
</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
{{-- </table> --}}
<!-- Modal Update-->
<div class="modal fade" id="update" tabindex="-1" aria-labelledby="update" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="update">Update</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form id="update" action="{{ route('form/update') }}" method = "post"><!-- form add -->
{{ csrf_field() }}
<div class="modal-body">
<input type="hidden" class="form-control" id="e_id" name="id" value=""/>
<div class="modal-body">
<div class="form-group row">
<label for="" class="col-sm-3 col-form-label">Full Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="e_name" name="name" required="" value=""/>
</div>
</div>
<div class="form-group row">
<label for="" class="col-sm-3 col-form-label">Email</label>
<div class="col-sm-9">
<input type="email" class="form-control" id="e_email" name="email" required="" value=""/>
</div>
</div>
<div class="form-group row">
<label for="" class="col-sm-3 col-form-label">Phone</label>
<div class="col-sm-9">
<input type="tel" class="form-control" id="e_phone" name="phone" required="" value=""/>
</div>
</div>
</div>
<!-- form add end -->
</div>
<div class="modal-footer">
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal"><i class="icofont icofont-eye-alt"></i>Close</button>
<button type="submit" id=""name="" class="btn btn-success waves-light"><i class="icofont icofont-check-circled"></i>Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
<!-- End Modal Update-->
{{-- script update --}}
<script>
$(document).on('click','.update',function()
{
var _this = $(this).parents('tr');
$('#e_id').val(_this.find('.id').text());
$('#e_name').val(_this.find('.name').text());
$('#e_email').val(_this.find('.email').text());
$('#e_phone').val(_this.find('.phone').text());
});
</script>
{{-- hide message js --}}
<script>
$('#insert').show();
setTimeout(function()
{
$('#insert').hide();
},5000);
$('#error').show();
setTimeout(function()
{
$('#error').hide();
},5000);
</script>
@endsection
Step 9: Run Development Server
After successfully update all run the development server just adding the below command in the terminal.
php artisan serve
After this go to the browser and check using the login page.
http://localhost/laravel_basic/public/
2 Comments
where are the other steps
ReplyDelete3
ReplyDeleteCAN FEEDBACK
Emoji