|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | + |
| 7 | +use DB; |
| 8 | +use Image; |
| 9 | + |
| 10 | +class FirstTable extends Controller |
| 11 | +{ |
| 12 | + public function store(Request $req){ |
| 13 | + $validated = $req->validate([ |
| 14 | + 'image' => ['image'] |
| 15 | + ], |
| 16 | + [ |
| 17 | + 'image.image' => 'Image must be an image' |
| 18 | + ]); |
| 19 | + |
| 20 | + $data = array(); |
| 21 | + $image = request()->file('image'); |
| 22 | + if($image){ |
| 23 | + $name = hexdec(uniqid()); |
| 24 | + $fullname = $name.'.webp'; |
| 25 | + $path = 'images/firsttable/'; |
| 26 | + $url = $path.$fullname; |
| 27 | + $resize_image=Image::make($image->getRealPath()); |
| 28 | + $resize_image->resize(300,300); |
| 29 | + $resize_image->save('images/firsttable/'.$fullname); |
| 30 | + $data['text'] = $req -> text; |
| 31 | + $data['number'] = $req -> number; |
| 32 | + $data['image'] = $url; |
| 33 | + DB::table('firsttable') -> insert($data); |
| 34 | + return response()->json([ |
| 35 | + 'message' => 'Data successfully added' |
| 36 | + ],200); |
| 37 | + } |
| 38 | + else{ |
| 39 | + $data['text'] = $req -> text; |
| 40 | + $data['number'] = $req -> number; |
| 41 | + DB::table('firsttable') -> insert($data); |
| 42 | + return response()->json([ |
| 43 | + 'message' => 'Data successfully added' |
| 44 | + ],200); |
| 45 | + } |
| 46 | + } |
| 47 | + public function show(){ |
| 48 | + $firsttables = DB::table('firsttable') -> orderBy('text', 'ASC') -> get(); |
| 49 | + return $firsttables; |
| 50 | + } |
| 51 | + public function edit($id){ |
| 52 | + $firsttable = DB::table('firsttable') -> where('id', $id)->first(); |
| 53 | + return $firsttable; |
| 54 | + } |
| 55 | + public function update(Request $req){ |
| 56 | + $validatedData = $req->validate([ |
| 57 | + 'image' => ['image'] |
| 58 | + ], |
| 59 | + [ |
| 60 | + 'image.image' => 'Image must be an image' |
| 61 | + ]); |
| 62 | + |
| 63 | + $data = array(); |
| 64 | + $image = request()->file('image'); |
| 65 | + if($image){ |
| 66 | + $firsttable = DB::table('firsttable') -> where('id', $req->id)->first(); |
| 67 | + $old_image=$firsttable->image; |
| 68 | + if(file_exists($old_image)){ |
| 69 | + unlink($old_image); |
| 70 | + } |
| 71 | + $name = hexdec(uniqid()); |
| 72 | + $fullname = $name.'.webp'; |
| 73 | + $path = 'images/firsttable/'; |
| 74 | + $url = $path.$fullname; |
| 75 | + $resize_image=Image::make($image->getRealPath()); |
| 76 | + $resize_image->resize(300,300); |
| 77 | + $resize_image->save('images/firsttable/'.$fullname); |
| 78 | + $data['text'] = $req -> text; |
| 79 | + $data['number'] = $req -> number; |
| 80 | + $data['image'] = $url; |
| 81 | + DB::table('firsttable') -> where('id', $req->id)->update($data); |
| 82 | + return response()->json([ |
| 83 | + 'message' => 'Data successfully updated' |
| 84 | + ],200); |
| 85 | + } |
| 86 | + else{ |
| 87 | + $data['text'] = $req -> text; |
| 88 | + $data['number'] = $req -> number; |
| 89 | + DB::table('firsttable') -> where('id', $req->id)->update($data); |
| 90 | + return response()->json([ |
| 91 | + 'message' => 'Data successfully updated' |
| 92 | + ],200); |
| 93 | + } |
| 94 | + } |
| 95 | + public function destroy(Request $req){ |
| 96 | + $firsttable = DB::table('firsttable') -> where('id', $req->id)->first(); |
| 97 | + $image = $firsttable->image; |
| 98 | + if(file_exists($image)){ |
| 99 | + unlink($image); |
| 100 | + } |
| 101 | + DB::table('firsttable') -> where('id', $req->id)->delete(); |
| 102 | + return response()->json([ |
| 103 | + 'message' => 'Data successfully deleted' |
| 104 | + ],200); |
| 105 | + } |
| 106 | +} |
0 commit comments