LARAVEL: EDIT SELECT2 TYPES [[ CRUD OPERATION ]]
TYPE 1:
CustomerController:
public function edit($id)
{
$customer = customer::find($id);
return view('customer.edit',compact('customer'));
}
public function update(Request $req, $id)
{
$ctmr = customer::find($id);
$ctmr->name = $req->name;
$ctmr->email = $req->email;
$ctmr->phone = $req->phone;
if ($req->hasFile('imgx')) {
unlink($ctmr->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);
$ctmr->image=$file_path;
}
$ctmr->save();
Session::flash('message', "Special Customer Records Are Updated Successfully");
return redirect('/');
}
TYPE 2:
CustomerController.php
public function editaction($id){
$data = customer::find($id);
return view('customer.editpage',['data'=>$data]);
}
public function updateaction(Request $req){
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);
// $ctmr->image=$file_path;
}else{
$file_path=$req->old_img;
}
$ctmr = DB::table('customers')
->where('id', $req->id)
->update([
'name' => $req->name,
'email' => $req->email,
'phone' => $req->phone,
'image' => $file_path,
]);
Session::flash('message', "Special Customer added Successfully");
return redirect('/');
}
FULL CRUD :
CustomerController.php >>
<?php
namespace App\Http\Controllers;
use App\Models\customer;
use Illuminate\Http\Request;
use Session;
use DB;
class CustomerController extends Controller
{
public function index()
{
$data=customer::all();
return view('customer.dashboard',compact('data'));
}
public function editaction($id){
$data = customer::find($id);
return view('customer.editpage',['data'=>$data]);
}
public function updateaction(Request $req){
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);
// $ctmr->image=$file_path;
}else{
$file_path=$req->old_img;
}
$ctmr = DB::table('customers')
->where('id', $req->id)
->update([
'name' => $req->name,
'email' => $req->email,
'phone' => $req->phone,
'image' => $file_path,
]);
Session::flash('message', "Special Customer added Successfully");
return redirect('/');
}
public function create(Request $req)
{
$ctmr = new customer;
$ctmr->name = $req->name;
$ctmr->email = $req->email;
$ctmr->phone = $req->phone;
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);
}
$ctmr->image=$file_path;
$ctmr->save();
Session::flash('message', "Special Customer added Successfully");
return back();
// ->with('success', 'Customer added Successfully');
}
public function show(customer $customer)
{
//
}
public function edit($id)
{
$customer = customer::find($id);
return view('customer.edit',compact('customer'));
}
public function update(Request $req, $id)
{
$ctmr = customer::find($id);
$ctmr->name = $req->name;
$ctmr->email = $req->email;
$ctmr->phone = $req->phone;
if ($req->hasFile('imgx')) {
unlink($ctmr->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);
$ctmr->image=$file_path;
}
$ctmr->save();
Session::flash('message', "Special Customer Records Are Updated Successfully");
return redirect('/');
}
public function destroy($id)
{
$ctmr = customer::find($id);
unlink($ctmr->image);
$ctmr->delete();
Session::flash('message', "Special Customer Records Are Delete Successfully");
return redirect('/');
}
}
Database >> Migrations >> create_customer_table.php
<?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('customers', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('email')->nullable();
$table->string('phone')->nullable();
$table->string('image')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('customers');
}
};
Make a folder in public directory. public >> images >> customer
Views >> Customer Folder
dashboard.blade.php
<!doctype html>
<html lang="en">
<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">
<title> CUSTOMER - DASHBOARD</title>
</head>
<body>
{{-- @if(session()->has('message')) <p class="alert alert-success">{{session('message')}}</p> @endif --}}
@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
<div class="row mt-5">
<div class="col-lg-4 col-md-8 mx-auto ">
<div class="container">
<h3>Registration Form</h3>
<form action="/forminsertaction" method="post" enctype="multipart/form-data">
@csrf
<div class="mb-3">
<label class="form-label">Name</label>
<input type="name" name="name" class="form-control">
</div>
<div class="mb-3">
<label class="form-label">Email</label>
<input type="email" name="email" class="form-control">
</div>
<div class="mb-3">
<label class="form-label">Phone No</label>
<input type="phone" name="phone" class="form-control">
</div>
<div class="mb-3">
<label class="form-label">Image</label>
<input type="file" name="imgx" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
<div class="col-lg-8 col-md-10 mx-auto ">
<div class="container">
<div class="mx-auto w-75">
<h3>Display</h3>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Phone</th>
<th scope="col">Image</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php $c=0; ?>
@foreach($data as $item)
<tr>
<th scope="row"> <?php echo ++$c; ?></th>
<td>{{$item->name}}</td>
<td>{{$item->email}}</td>
<td>{{$item->phone}}</td>
<td> <img src="{{$item->image}}" height="80px" width="100px"> </td>
<td>
<a href="/edit/{{$item->id}}"> EDIT </a>
<a href="/delete/{{$item->id}}"> DELETE </a>
</td>
</tr>
@endforeach
</tbody>
</table>
</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>
{{-- <a href="/edit/{{$item->id}}"> EDIT </a> --}}
Edit.blade.php
<!doctype html>
<html lang="en">
<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">
<title> CUSTOMER - EDIT</title>
</head>
<body>
<div class="row mt-5">
<div class="col-md-6 mx-auto ">
<div class="container">
<h3>Updation Form</h3>
<form action="{{url('/formeditaction/'.$customer->id)}} " method="post" enctype="multipart/form-data">
@csrf
<div class="mb-3">
<label class="form-label">Name</label>
<input type="name" name="name" class="form-control" value="{{$customer->name}}">
</div>
<div class="mb-3">
<label class="form-label">Email</label>
<input type="email" name="email" class="form-control" value="{{$customer->email}}">
</div>
<div class="mb-3">
<label class="form-label">Phone No</label>
<input type="phone" name="phone" class="form-control" value="{{$customer->phone}}">
</div>
<div class="mb-3">
<label class="form-label">Image</label>
<input type="file" name="imgx" class="form-control">
<img src="../{{$customer->image}}" height="120px" width="100px" class="p-2 my-2 mx-5">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</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>
editpage.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Cafe</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>
</head>
<body>
<div class="container">
<h3>Update Form</h3>
<form action="/updateaction" method="post" enctype="multipart/form-data">
@csrf
<input type="hidden" name="id" class="form-control" value="{{$data->id}}">
<input type="hidden" name="old_img" class="form-control" value="{{$data->image}}">
<div class="mb-3">
<label class="form-label">Name</label>
<input type="text" name="name" class="form-control" value="{{$data->name}}">
</div>
<div class="mb-3">
<label class="form-label">Email</label>
<input type="email" name="email" class="form-control" value="{{$data->email}}">
</div>
<div class="mb-3">
<label class="form-label">Phone No</label>
<input type="number" name="phone" class="form-control" value="{{$data->phone}}">
</div>
<div class="mb-3">
<label class="form-label">Image</label>
<input type="file" name="imgx" class="form-control">
<img src="../{{$data->image}}" alt="" srcset="" height="100px">
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</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>
Route >> web.php
Route::get('/',[CustomerController::class,'index'])->name('index');
Route::post('/forminsertaction',[CustomerController::class,'create'])->name('forminsertaction');
// Route::get('/edit/{id}',[CustomerController::class,'edit'])->name('edit');
// /formeditaction
// Route::post('/formeditaction/{id}',[CustomerController::class,'update'])->name('formeditaction');
Route::get('/delete/{id}',[CustomerController::class,'destroy'])->name('delete');
Route::get('/edit/{id}',[CustomerController::class,'editaction'])->name('edit');
Route::post('/updateaction',[CustomerController::class,'updateaction'])->name('updateaction');
Comments
Post a Comment