after the execution of the above command, you will find your model, migration and controller of Student.
After creating all the fields in your above students table, let's give a command to migrate into the database as follows.
<?php
namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class StudentController extends Controller
{
public function index()
{
return view('student.index');
}
public function fetchstudent()
{
$students = Student::all();
return response()->json([
'students'=>$students,
]);
}
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name'=> 'required|max:191',
'course'=>'required|max:191',
'email'=>'required|email|max:191',
'phone'=>'required|max:10|min:10',
]);
if($validator->fails())
{
return response()->json([
'status'=>400,
'errors'=>$validator->messages()
]);
}
else
{
$student = new Student;
$student->name = $request->input('name');
$student->course = $request->input('course');
$student->email = $request->input('email');
$student->phone = $request->input('phone');
$student->save();
return response()->json([
'status'=>200,
'message'=>'Student Added Successfully.'
]);
}
}
public function edit($id)
{
$student = Student::find($id);
if($student)
{
return response()->json([
'status'=>200,
'student'=> $student,
]);
}
else
{
return response()->json([
'status'=>404,
'message'=>'No Student Found.'
]);
}
}
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
'name'=> 'required|max:191',
'course'=>'required|max:191',
'email'=>'required|email|max:191',
'phone'=>'required|max:10|min:10',
]);
if($validator->fails())
{
return response()->json([
'status'=>400,
'errors'=>$validator->messages()
]);
}
else
{
$student = Student::find($id);
if($student)
{
$student->name = $request->input('name');
$student->course = $request->input('course');
$student->email = $request->input('email');
$student->phone = $request->input('phone');
$student->update();
return response()->json([
'status'=>200,
'message'=>'Student Updated Successfully.'
]);
}
else
{
return response()->json([
'status'=>404,
'message'=>'No Student Found.'
]);
}
}
}
public function destroy($id)
{
$student = Student::find($id);
if($student)
{
$student->delete();
return response()->json([
'status'=>200,
'message'=>'Student Deleted Successfully.'
]);
}
else
{
return response()->json([
'status'=>404,
'message'=>'No Student Found.'
]);
}
}
}
@extends('layouts.app')
@section('content')
{{-- Add Modal --}}
<div class="modal fade" id="AddStudentModal" tabindex="-1" aria-labelledby="AddStudentModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="AddStudentModalLabel">Add Student Data</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<ul id="save_msgList"></ul>
<div class="form-group mb-3">
<label for="">Full Name</label>
<input type="text" required class="name form-control">
</div>
<div class="form-group mb-3">
<label for="">Course</label>
<input type="text" required class="course form-control">
</div>
<div class="form-group mb-3">
<label for="">Email</label>
<input type="text" required class="email form-control">
</div>
<div class="form-group mb-3">
<label for="">Phone No</label>
<input type="text" required class="phone form-control">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary add_student">Save</button>
</div>
</div>
</div>
</div>
{{-- Edit Modal --}}
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editModalLabel">Edit & Update Student Data</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<ul id="update_msgList"></ul>
<input type="hidden" id="stud_id" />
<div class="form-group mb-3">
<label for="">Full Name</label>
<input type="text" id="name" required class="form-control">
</div>
<div class="form-group mb-3">
<label for="">Course</label>
<input type="text" id="course" required class="form-control">
</div>
<div class="form-group mb-3">
<label for="">Email</label>
<input type="text" id="email" required class="form-control">
</div>
<div class="form-group mb-3">
<label for="">Phone No</label>
<input type="text" id="phone" required class="form-control">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary update_student">Update</button>
</div>
</div>
</div>
</div>
{{-- Edn- Edit Modal --}}
{{-- Delete Modal --}}
<div class="modal fade" id="DeleteModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Student Data</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<h4>Confirm to Delete Data ?</h4>
<input type="hidden" id="deleteing_id">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary delete_student">Yes Delete</button>
</div>
</div>
</div>
</div>
{{-- End - Delete Modal --}}
<div class="container py-5">
<div class="row">
<div class="col-md-12">
<div id="success_message"></div>
<div class="card">
<div class="card-header">
<h4>
Student Data
<button type="button" class="btn btn-primary float-end" data-bs-toggle="modal"
data-bs-target="#AddStudentModal">Add Student</button>
</h4>
</div>
<div class="card-body">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Course</th>
<th>Email</th>
<th>Phone</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
<script>
$(document).ready(function () {
fetchstudent();
function fetchstudent() {
$.ajax({
type: "GET",
url: "/fetch-students",
dataType: "json",
success: function (response) {
// console.log(response);
$('tbody').html("");
$.each(response.students, function (key, item) {
$('tbody').append('<tr>\
<td>' + item.id + '</td>\
<td>' + item.name + '</td>\
<td>' + item.course + '</td>\
<td>' + item.email + '</td>\
<td>' + item.phone + '</td>\
<td><button type="button" value="' + item.id + '" class="btn btn-primary editbtn btn-sm">Edit</button></td>\
<td><button type="button" value="' + item.id + '" class="btn btn-danger deletebtn btn-sm">Delete</button></td>\
\</tr>');
});
}
});
}
$(document).on('click', '.add_student', function (e) {
e.preventDefault();
$(this).text('Sending..');
var data = {
'name': $('.name').val(),
'course': $('.course').val(),
'email': $('.email').val(),
'phone': $('.phone').val(),
}
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "POST",
url: "/students",
data: data,
dataType: "json",
success: function (response) {
// console.log(response);
if (response.status == 400) {
$('#save_msgList').html("");
$('#save_msgList').addClass('alert alert-danger');
$.each(response.errors, function (key, err_value) {
$('#save_msgList').append('<li>' + err_value + '</li>');
});
$('.add_student').text('Save');
} else {
$('#save_msgList').html("");
$('#success_message').addClass('alert alert-success');
$('#success_message').text(response.message);
$('#AddStudentModal').find('input').val('');
$('.add_student').text('Save');
$('#AddStudentModal').modal('hide');
fetchstudent();
}
}
});
});
$(document).on('click', '.editbtn', function (e) {
e.preventDefault();
var stud_id = $(this).val();
// alert(stud_id);
$('#editModal').modal('show');
$.ajax({
type: "GET",
url: "/edit-student/" + stud_id,
success: function (response) {
if (response.status == 404) {
$('#success_message').addClass('alert alert-success');
$('#success_message').text(response.message);
$('#editModal').modal('hide');
} else {
// console.log(response.student.name);
$('#name').val(response.student.name);
$('#course').val(response.student.course);
$('#email').val(response.student.email);
$('#phone').val(response.student.phone);
$('#stud_id').val(stud_id);
}
}
});
$('.btn-close').find('input').val('');
});
$(document).on('click', '.update_student', function (e) {
e.preventDefault();
$(this).text('Updating..');
var id = $('#stud_id').val();
// alert(id);
var data = {
'name': $('#name').val(),
'course': $('#course').val(),
'email': $('#email').val(),
'phone': $('#phone').val(),
}
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "PUT",
url: "/update-student/" + id,
data: data,
dataType: "json",
success: function (response) {
// console.log(response);
if (response.status == 400) {
$('#update_msgList').html("");
$('#update_msgList').addClass('alert alert-danger');
$.each(response.errors, function (key, err_value) {
$('#update_msgList').append('<li>' + err_value +
'</li>');
});
$('.update_student').text('Update');
} else {
$('#update_msgList').html("");
$('#success_message').addClass('alert alert-success');
$('#success_message').text(response.message);
$('#editModal').find('input').val('');
$('.update_student').text('Update');
$('#editModal').modal('hide');
fetchstudent();
}
}
});
});
$(document).on('click', '.deletebtn', function () {
var stud_id = $(this).val();
$('#DeleteModal').modal('show');
$('#deleteing_id').val(stud_id);
});
$(document).on('click', '.delete_student', function (e) {
e.preventDefault();
$(this).text('Deleting..');
var id = $('#deleteing_id').val();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "DELETE",
url: "/delete-student/" + id,
dataType: "json",
success: function (response) {
// console.log(response);
if (response.status == 404) {
$('#success_message').addClass('alert alert-success');
$('#success_message').text(response.message);
$('.delete_student').text('Yes Delete');
} else {
$('#success_message').html("");
$('#success_message').addClass('alert alert-success');
$('#success_message').text(response.message);
$('.delete_student').text('Yes Delete');
$('#DeleteModal').modal('hide');
fetchstudent();
}
}
});
});
});
</script>
@endsection
Now we have successfully completed with it guys, now lets run the application by following command: