LARAVEL: SELECT2 DATATABLES MODEL JOINING TABLES SINGLE IMG JQUERY FORM VALIDATION [[ Ultimate Crud ]]

<!-- index.blade.php [ Views ] -->
<!doctype html>
<html lang="en">
    {{-- name, email, phone, address, education select2, qualification, profile_img --}}
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <title>DATE: 26/9/22</title>

    <!--  -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>        
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <!--  -->

    <!--  -->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.css">
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.js"></script>
    <!--  -->
</head>

<body style=" font-family: 'Julius Sans One', sans-serif; background:linear-gradient(to right,#e4bcf39f,#c4fdd585); background-size: 100vw 100vh;  background-repeat: no-repeat;  background-attachment: fixed; ">
    {{-- SESSION MESSAGES --}}

    @if (Session::has('message'))
      <div class="alert alert-info alert-dismissible fade show" role="alert">
        <strong> {{ Session::get('message') }}</strong>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
      </div>
    @endif
    @if($errors->any())
        <div class="alert alert-warning alert-dismissible fade show"  role="alert">
            <nav class="navbar navbar-expand-sm sticky-top">
            @foreach($errors->all() as $err)
            <ul class="navbar-nav">
                <li class="nav-item p-1"> <i class="fa-solid fa-oil-well"> </i> {{$err}} </li>
            </ul>
            @endforeach
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </nav>
        </div>
    @endif

{{-- name, email, phone, address, education select2, qualification, profile_img --}}
<div class="row">
    <div class="col-md-8 mx-auto mt-2 mb-5 ">

        <div class="p-3 nav justify-content-end ">
            <button type="button " class="btn btn-outline-success " data-bs-toggle="modal" data-bs-target="#recordsModal">
                ADD RECORDS
            </button>  
            <button type="button " class="btn btn-outline-primary mx-3  " data-bs-toggle="modal" data-bs-target="#eduModal">
                ADD EDUCATION
            </button>            
        </div>
        {{-- ADD --}}
        <!-- Button trigger modal -->
        {{-- @if($errors->any())
        @foreach($errors->all() as $err)
            <li>{{$err}}</li>
        @endforeach
        @endif --}}
        <!-- Modal -->
        <div class="modal fade" id="recordsModal" 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">ADD RECORDS</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                   
                    {{-- form --}}
                    <form method="post" action="/forminsertaction" enctype="multipart/form-data" id="basic-form">
                        @csrf
                        <div class="mb-2 form-group">
                            <label for="" class="form-label">Name</label>
                            <input type="text" class="input-sm form-control" id="name" name="name">
                            <span class="text-danger">@error('name'){{$message}} @enderror </span>
                        </div>
                        <div class="mb-2 form-group">
                            <label for="" class="form-label">Email address</label>
                            <input type="email" name="email" id="email" class="form-control">
                            <span class="text-danger">@error('email'){{$message}} @enderror </span>
                        </div>
                        <div class="mb-2 form-group">
                            <label class="form-label">Phone</label>
                            <input type="number" name="phone"  id="phone" class="form-control">
                            <span class="text-danger">@error('phone'){{$message}} @enderror </span>
                        </div>
                        <div class="mb-2 form-group">
                            <label class="form-label">Address</label>
                            <input type="text" name="address" id="address" class="form-control">
                            <span class="text-danger">@error('address'){{$message}} @enderror </span>
                        </div>
                        <div class="mb-2 form-group" id="myModal" >
                            <label class="form-label">Education</label>
                            <select name="edu[]" id="mySelect2"  class="form-control" multiple="multiple"  style="width:30vw !important;">
                                <option>Select</option>
                                @foreach($data as $i)
                                 <option value="{{$i->id}}">{{$i->title}}</option>
                                @endforeach
                            </select>    
                            <span class="text-danger">@error('edu_id'){{$message}} @enderror </span>                        
                        </div>        
                        <script>
                            $(document).ready(function() {
                            //  $('.js-example-basic-multiple').select2();
                            }   );
                        </script>  
                        <script>
                            $('#mySelect2').select2({
                                dropdownParent: $('#myModal')
                            });
                        </script>        
                                               
                        <div class="my-2 form-group">
                            <label class="form-label">Add Image</label>
                            <input type="file" name="imgx" id="imgx">
                            <span class="text-danger">@error('imgx'){{$message}} @enderror </span>  
                        </div>
                        <div class="mb-2 modal-footer">
                            <button type="submit" class="btn  btn-primary">Submit</button>
                            <button type="reset" class="btn  btn-primary">Reset</button>
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                        </div>
                    </form>

                </div>
                {{-- <div class="modal-footer">                </div> --}}
                <style>
                   
                    label.error {
                        color: red;
                        font-size: 1rem;
                        display: block;
                        margin-top: 5px;
                    }

                    label.error.fail-alert {
                        border: 2px solid red;
                        border-radius: 4px;
                        line-height: 1;
                        padding: 2px 0 6px 6px;
                        background: #ffe6eb;
                    }

                    input.valid.success-alert {
                        border: 2px solid #4CAF50;
                        color: green;
                    }
                </style>
                <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
                <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
                <script>
                    $(document).ready(function() {
                        $("#basic-form").validate({
                         
                            errorClass: "error fail-alert",
                            validClass: "valid success-alert",
                            rules: {
                                name : {
                                    required: true,
                                    minlength: 3
                                },
                                phone: {
                                    required: true,
                                    number: true,
                                    max: 9999999999,  
                                    min: 1000000000                              
                                },
                                email: {
                                    required: true,
                                    email: true
                                },                                
                                address: {
                                    required: true,                                    
                                },
                                imgx: {
                                    required: true,    
                                    extension: "jpg|jpeg|png|ico|bmp"
                                },
                             
                            },
                            messages : {
                            name: {
                                minlength: "Name should be at least 3 characters"
                            },
                            phone: {
                                required: "Please enter your phone no",
                                number: "Please enter your phone number as a numerical value",
                                min: "You must be at least 10 digits",
                                max: "You must be not greater than 10 digits",
                            },
                            email: {
                                email: "The email should be in the format: abc@domain.tld"
                            },
                            address: {
                                required: "Please fill address",                              
                            },
                            imgx: {
                                required: "Please upload file.",
                                extension: "Please upload file in these format only (jpg, jpeg, png, ico, bmp)."
                            }
                            }
                        });
                        $("#basic-form2").validate({
                            errorClass: "error fail-alert",
                            validClass: "valid success-alert",
                            rules: {
                                title : {
                                    required: true,
                                    minlength: 1
                                },
                            },
                            messages : {
                                title: {
                                minlength: "Language should be at least 1 characters"
                            }
                            }
                        });
                    });
                </script>
            </div>
            </div>
        </div>
        {{-- edu model  --}}
        <!-- Modal -->
        <div class="modal fade" id="eduModal" tabindex="-1" aria-labelledby="eduModalLabel" aria-hidden="true">
            <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">ADD EDUCATIONS</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form action="/eduadding" method="POST" id="basic-form2">
                        @csrf
                        <div class="input-group">
                            <input type="text" name="title" id="title" class="form-control" placeholder="Add education here">
                            <button type="submit" class="input-group-text btn btn-info" >Add </button>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>                
                </div>
            </div>
            </div>
        </div>
        {{-- DATA TABLES --}}
        <table class="table align-middle table-hover table-sm" id="table_id">
            <thead>
                <tr>
                    <th scope="col">#</th>
                    <th scope="col">Name</th>
                    <th scope="col">Email</th>
                    <th scope="col">Phone</th>
                    <th scope="col">Address</th>
                    <th scope="col">Education</th>                    
                    <th scope="col">Image</th>
                    <th scope="col">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php $c=0; ?>
                @foreach($benall as $i)
                <tr>                  
                    <td scope="col"><?php echo ++$c; ?></td>
                    <td scope="col">{{ $i['name']}}</td>
                    <td scope="col">{{$i['email']}}</td>
                    <td scope="col">{{$i['phone']}}</td>
                    <td scope="col">{{$i['address']}}</td>
                    <td scope="col">{{$i['education']}}</td>
                    <td scope="col"> <img src="{{$i['image']}}" height="35vh" alt=""> </td>
                    <td scope="col">
                        <a href="{{route('editz',['id'=>$i['id']])}} " class="btn btn-outline-warning"><i class="fa-solid fa-user-pen"></i></a>
                        <a href="/delete/{{$i['id']}}" class="btn btn-outline-danger"><i class="fa-solid fa-trash-can"></i></a>
                    </td>                    
                </tr>
                @endforeach
            </tbody>
        </table>                  
        <script>
        $(document).ready( function () {
            $('#table_id').DataTable();
        } );
        </script>

    </div>    
</div>
   
    <script src = "https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"   integrity = "sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"     crossorigin = "anonymous"></script>
 
</body>
</html>

Web.php [ Route]

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HeatBlastController;

Route::get('/',[HeatBlastController::class,'index'])->name('index');
Route::post('/eduadding',[HeatBlastController::class,'eduadd'])->name('eduadding');
Route::post('/forminsertaction',[HeatBlastController::class,'forminsertaction'])->name('forminsertaction');
Route::get('/delete/{id}',[HeatBlastController::class,'del'])->name('delete');
Route::get('/edit/{id}',[HeatBlastController::class,'editz'])->name('editz');
Route::post('/formeditaction',[HeatBlastController::class,'formeditaction'])->name('formeditaction');
// forminsertaction

HeatBlastController.php [ Controller ]

<?php

namespace App\Http\Controllers;

use App\Models\HeatBlast;
use App\Models\education;
use Illuminate\Http\Request;
use DB;
use Session;
use Illuminate\Support\Arr;

class HeatBlastController extends Controller
{
    public function eduadd(Request $req){
        // $ctmr =  DB::table('education')->Insert(['title' => $req->title],);
        $req->validate([
            'title' => 'required',          
        ]);
        $ctmr = new education;
        $ctmr->title = $req->title;        
        $ctmr->save();
        Session::flash('message', "Education added Successfully");        
        return redirect('/');
    }

    public function forminsertaction(Request $req){        
    //  name, email, phone, address, education select2, qualification, profile_img

       $req->validate([
            'name' => 'required',
            'email' => 'required |unique:heat_blasts',
            'phone' => 'required | min:10 |max:10 |unique:heat_blasts',
            'address' => 'required',
            'edu' => 'required',
        ]);

        $ben = new HeatBlast;
        $ben->name = $req->name;
        $ben->email = $req->email;
        $ben->phone = $req->phone;
        $ben->address = $req->address;
        $ben->edu_id =  implode(', ',$req->edu);
        if ($req->hasFile('imgx')) {
            $file_type = $req->file('imgx')->extension();
            $file_path = $req->file('imgx')->storeAs('images/customer', time() . '.' . $file_type, 'public');
            $req->file('imgx')->move(public_path('images/customer'), time() . '.' . $file_type);
        }
        $ben->image=$file_path;      
        Session::flash('message', "Records are Added Successfully");
        $ben->save();
        return redirect('/');
    }

    public function index()
    {        
        $eduall = DB::table('education')->get();
        $benall = DB::table('heat_blasts')->get();
        $multipledata=array();
        $temp=array();
        foreach($benall as $ben){
            //  name, email, phone, address, education select2, qualification, profile_img
            // $array = Arr::add(['name' => 'Desk'], 'price', 100);
            $temp['id']=$ben->id;
            $temp['name']=$ben->name;
            $temp['email']=$ben->email;
            $temp['phone']=$ben->phone;
            $temp['address']=$ben->address;
            $temp['image']=$ben->image;          
            $flag = explode(', ',$ben->edu_id);                        
            $demo=array();
            foreach($flag as $a){
                $ctmr = education::find($a);
                   array_push($demo,$ctmr->title);  
            }        
            $temp['education']= implode(', ', $demo);
            array_push($multipledata, $temp);
        }        
        return view('index',['data'=>$eduall,'benall'=>$multipledata]);
    }

 
    public function del($id)
    {
        $bendel = HeatBlast::find($id);
        unlink($bendel->image);
        $bendel->delete();
       
        Session::flash('message', " Records Are Delete Successfully");
        return redirect('/');        
    }

    public function editz($id){
        $ben = HeatBlast::find($id);
        $eduall = education::all();
        $data2 = education::select("*")->whereIn('id', explode(', ',$ben->edu_id))->get();
        return view('editpage',['data'=>$ben, 'eduall'=>$eduall,'data2'=>$data2]);
    }

    public function formeditaction(Request $req){
        $req->validate([
            'name' => 'required',
            'email' => 'required |unique:heat_blasts',
            'phone' => 'required | min:10 |max:10 |unique:heat_blasts',
            'address' => 'required',            
        ]);

        if ($req->hasFile('imgx')) {
            if($req->image !='' )
                unlink($req->image);
            $file_type = $req->file('imgx')->extension();
            $file_path = $req->file('imgx')->storeAs('images/customer', time() . '.' . $file_type, 'public');
            $req->file('imgx')->move(public_path('images/customer'), time() . '.' . $file_type);            
        }else{
            $file_path=$req->old_img;
        }

        $ctmr =  DB::table('heat_blasts')
        ->where('id', $req->eid)
        ->update([
            'name' => $req->name,
            'email' => $req->email,
            'phone' => $req->phone,
            'address' => $req->address,
            'edu_id' => implode(', ',$req->edu),
            'image' => $file_path,
        ]);

        Session::flash('message', "Records Updated Successfully");
        return redirect('/');
    }
}

editpage.blade.php [Views]

<!doctype html>
<html lang="en">
    {{-- name, email, phone, address, education select2, qualification, profile_img --}}
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <title>DATE: 26/9/22</title>

    <!--  -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>        
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <!--  -->

    <!--  -->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.css">
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.js"></script>
    <!--  -->
</head>
<body style="background:linear-gradient(to right, rgb(221, 168, 210) , rgb(11, 71, 99))">
   
<div class="row">
    <div class="col-4 mx-auto my-4 " >
        <div class="card mb-3 p-2" style="max-width: 540px; border-radius: 5px 50px; ">
            <div class="row g-0">
              <div class="col-md-4">
                {{-- <img src="../{{$data->image}}" alt="" height="140px" width="200px"> --}}
                <label for="" class="p-3 fw-bold">Previous Image:</label>
                <img src="../{{$data->image}}" class="img-fluid rounded-start p-2" alt="...">
                {{-- <label for="" class="p-3 fst-italic">{{$data->image}}</label> --}}
              </div>
              <div class="col-md-8">
                <div class="card-body">
                 
                    {{-- form --}}
                    <form method="post" id="basic-form2" action="/formeditaction" enctype="multipart/form-data">
                        @csrf
                        <input type="hidden" name="eid" value="{{$data->id}}">
                        <input type="hidden" name="old_img" value="{{$data->image}}">                        
                        <div class="mb-2 form-group">
                            <label for="" class="form-label fw-bold">Name</label>
                            <input type="text" class="input-sm form-control" name="name" id="name" value="{{$data->name}}">
                            <span class="text-danger">@error('name'){{$message}} @enderror </span>  
                        </div>
                        <div class="mb-2 form-group">
                            <label for="" class="form-label fw-bold">Email address</label>
                            <input type="email" name="email" id="email" class="form-control" value="{{$data->email}}">
                            <span class="text-danger">@error('email'){{$message}} @enderror </span>  
                        </div>
                        <div class="mb-2 form-group">
                            <label class="form-label  fw-bold">Phone</label>
                            <input type="number" name="phone" id="phone" class="form-control" value="{{$data->phone}}">
                            <span class="text-danger">@error('phone'){{$message}} @enderror </span>  
                        </div>
                        <div class="mb-2 form-group">
                            <label class="form-label fw-bold">Address</label>
                            <input type="text" name="address" id="address" class="form-control" value="{{$data->address}}">
                            <span class="text-danger">@error('address'){{$message}} @enderror </span>  
                        </div>
                        <div class="mb-2 form-group" id="myModal" >
                            <label class="form-label fw-bold">Education</label>
                            <select name="edu[]" id="mySelect2"  class="form-control" multiple="multiple"  >
                                <option>Select</option>
                                @foreach($eduall as $edualex)                                    
                                        <option value="{{ $edualex->id}}"@foreach($data2 as $eduone) @if($eduone->id==$edualex->id) selected @endif @endforeach> {{$edualex->title}} </option>                                    
                                @endforeach
                            </select>  
                            <span class="text-danger">@error('edu_id'){{$message}} @enderror </span>                        
                        </div>                                
                        <script>
                            $(document).ready(function() {
                            //  $('.js-example-basic-multiple').select2();
                            }   );
                        </script>  
                        <script>
                            $('#mySelect2').select2({
                                dropdownParent: $('#myModal')
                            });
                        </script>        
                                           
                        <div class="my-2 form-group">
                            <label class="form-label fw-bold">Add Image</label>
                            <input type="file" name="imgx">
                           
                           
                        </div>
                        <div class="mb-2 modal-footer">
                            <button type="submit" class="btn  btn-primary">Submit</button>                            
                            <a href="/" class="btn btn-secondary" data-bs-dismiss="modal">Back</a>
                        </div>
                    </form>
                    <style>
                   
                        label.error {
                            color: red;
                            font-size: 1rem;
                            display: block;
                            margin-top: 5px;
                        }
   
                        label.error.fail-alert {
                            border: 2px solid red;
                            border-radius: 4px;
                            line-height: 1;
                            padding: 2px 0 6px 6px;
                            background: #ffe6eb;
                        }
   
                        input.valid.success-alert {
                            border: 2px solid #4CAF50;
                            color: green;
                        }
                    </style>
                    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
                    <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
                    <script>
                        $(document).ready(function() {
                            $("#basic-form2").validate({
                             
                                errorClass: "error fail-alert",
                                validClass: "valid success-alert",
                                rules: {
                                    name : {
                                        required: true,
                                        minlength: 3
                                    },
                                    phone: {
                                        required: true,
                                        number: true,
                                        max: 9999999999,  
                                        min: 1000000000                              
                                    },
                                    email: {
                                        required: true,
                                        email: true
                                    },                                
                                    address: {
                                        required: true,                                    
                                    },
                                    imgx: {                                      
                                        extension: "jpg|jpeg|png|ico|bmp"
                                    },
                                 
                                },
                                messages : {
                                name: {
                                    minlength: "Name should be at least 3 characters"
                                },
                                phone: {
                                    required: "Please enter your phone no",
                                    number: "Please enter your phone number as a numerical value",
                                    min: "You must be at least 10 digits",
                                    max: "You must be not greater than 10 digits",
                                },
                                email: {
                                    email: "The email should be in the format: abc@domain.tld"
                                },
                                address: {
                                    required: "Please fill address",                              
                                },
                                imgx: {                                  
                                    extension: "Please upload file in these format only (jpg, jpeg, png, ico, bmp)."
                                }
                                }
                            });
                           
                        });
                    </script>
                </div>
              </div>
            </div>
          </div>
    </div>
</div>

<script src = "https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"   integrity = "sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"     crossorigin = "anonymous"></script>

</body>
</html>

education.php [ migration table  ]

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('education', function (Blueprint $table) {
            $table->id();
            $table->string('title')->nullable();            
            $table->timestamps();
        });
    }
   
    public function down()
    {
        Schema::dropIfExists('education');
    }
};

heat_blast [ migration table ]

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{

    public function up()
    {
        Schema::create('heat_blasts', function (Blueprint $table) {
            $table->id();
            // name, email, phone, address, education select2,  image
            $table->string('name')->nullable();
            $table->string('email')->nullable();
            $table->bigInteger('phone')->nullable();
            $table->string('address')->nullable();
            $table->string('edu_id')->nullable();            
            $table->string('image')->nullable();

            $table->timestamps();
        });
    }
 
    public function down()
    {
        Schema::dropIfExists('heat_blasts');
    }
};

DB: ultimatecrud [ maintain it in .env file ]


-----------------------------------------------------------------------------------------------------------------------------

Another controller for some different methods apply: For different functions and views

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Fincrud;
use Illuminate\Support\Facades\DB;

class FincrudController extends Controller
{
    //
    public function fincrud_create()
    {  
        $data1=DB::table('eduqualification')->select('*')->get();
        $data2=DB::table('fincruds')
                   ->join('eduqualification','fincruds.education','=','eduqualification.edu_id')
                   ->select('*')
                   ->get();
                //    print '<pre>';
                //    print_r($data2);
                //    die;
        return view('fincrud',['data1'=>$data1,'data2'=>$data2]);
    }

    public function fincrud_post(Request $request)
     {
        $fincrud=new Fincrud;
        if ($request->hasFile('profile_image')) {
            $file_type = $request->file('profile_image')->extension();
            $file_path = $request->file('profile_image')->storeAs('images/users', time() . '.' . $file_type, 'public');
            $request->file('profile_image')->move(public_path('images/users'), time() . '.' . $file_type);
        }

        $fincrud->name=$request->name;
        $fincrud->email=$request->email;
        $fincrud->phone=$request->phone;
        $fincrud->education=implode(',',$request->edu);
        $fincrud->address=$request->address;
        $fincrud->profile_image=$file_path;
        $fincrud->save();
        return back()->with('status','One Record Added Successfully!!');
     }

     public function fincrud_delete($id)
     {
        $fincrud=Fincrud::find($id);
        if (!is_null($fincrud)) {
            unlink($fincrud->profile_image);
            $fincrud->delete();
        }
     
        return back()->with('status','One Record Deleted Successfully!!');

     }

     public function fincrud_edit($id)
     {
        $fincrud=Fincrud::find($id);
        $data3=DB::table('eduqualification')->select('*')->get();

        $data4=DB::table('fincruds')
                   ->select('fincruds.education')
                   ->where('id',$id)->first();

        return view('editFincrud',['fincrud' => $fincrud,'data3' => $data3,'data4' => $data4]);
       
     }

     public function fincrud_update(Request $request)
     {
        $fincrud=Fincrud::find($request->id);

        if ($request->hasFile('profile_image')) {
            unlink($fincrud->profile_image);
            $file_type = $request->file('profile_image')->extension();
            $file_path = $request->file('profile_image')->storeAs('images/users', time() . '.' . $file_type, 'public');
            $request->file('profile_image')->move(public_path('images/users'), time() . '.' . $file_type);
        }
        $fincrud->name=$request->name;
        $fincrud->email=$request->email;
        $fincrud->phone=$request->phone;
        $fincrud->education=implode(',',$request->edu);
        $fincrud->address=$request->address;
        $fincrud->profile_image=$file_path;
        $fincrud->save();
        return redirect(route('fincrud_create'))->with('status','One Record Updated Successfully!!');
     }

}


//

// <td>
// @foreach ($data1 as $item1)
//     @foreach ($ed as $item2)
//         @if ($item2 == $item1->edu_id)
//             {{ $item1->edu_name }}
//         @endif
//     @endforeach
// @endforeach

// </td>


Make a folder in public folder  [ images/customer ]




Comments

Popular posts from this blog

[[ ROYAL CAR ]] CHANGE PASSWORD - DYNAMIC BANNER - MULTIPLE IMAGE - LOGIN LOGOUT BACK BUTTON MIDDLEWARE STOP - MAIL DIRECTLY WITH FEEDBACK WITH SAVE IN SQL DB - ADMIN REPLY EXISTING MAILS - DYNAMICALLY CSS CHANGE

Sahari

Linux Terminal