|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use App\Models\Testinput; |
| 7 | +use App\Models\Secondtestinput; |
| 8 | +use Image; |
| 9 | + |
| 10 | +class Inputtest extends Controller |
| 11 | +{ |
| 12 | + public function add(Request $req){ |
| 13 | + $validatedData = $req->validate([ |
| 14 | + 'text' => ['required', 'string', 'max:255'], |
| 15 | + 'number' => ['required', 'integer', 'min:1'], |
| 16 | + 'image' => ['image', 'max:2048'], |
| 17 | + ], |
| 18 | + [ |
| 19 | + 'text.required' => 'Text is required', |
| 20 | + 'text.string' => 'Text must be a string', |
| 21 | + 'text.max' =>'Text must not be greater than 255 characters', |
| 22 | + 'number.required' => 'Number is required', |
| 23 | + 'number.integer' => 'Number must be an integer', |
| 24 | + 'number.min' => 'Number must be at least 1', |
| 25 | + 'image.image' => 'Image must be an image', |
| 26 | + ]); |
| 27 | + |
| 28 | + $testinput = new Testinput; |
| 29 | + $image = request()->file('image'); |
| 30 | + if($image){ |
| 31 | + $name = hexdec(uniqid()); |
| 32 | + $fullname = $name.'.webp'; |
| 33 | + $path = 'images/testinputs/images/'; |
| 34 | + $url = $path.$fullname; |
| 35 | + $resize_image=Image::make($image->getRealPath()); |
| 36 | + $resize_image->resize(300,300); |
| 37 | + $resize_image->save('images/testinputs/images/'.$fullname); |
| 38 | + $testinput -> text = $req -> text; |
| 39 | + $testinput -> number = $req -> number; |
| 40 | + $testinput -> image = $url; |
| 41 | + $testinput -> save(); |
| 42 | + return redirect()->route('welcome')->with('message','Data successfully added'); |
| 43 | + } |
| 44 | + else{ |
| 45 | + $testinput -> text = $req -> text; |
| 46 | + $testinput -> number = $req -> number; |
| 47 | + $testinput -> save(); |
| 48 | + return redirect()->route('welcome')->with('message','Data successfully added'); |
| 49 | + } |
| 50 | + } |
| 51 | + public function view(){ |
| 52 | + $views = Testinput::orderBy('text', 'ASC')->get(); |
| 53 | + return view('view', compact('views')); |
| 54 | + } |
| 55 | + public function update($id){ |
| 56 | + $update = Testinput::findorfail($id); |
| 57 | + return view('update', ['update' => $update]); |
| 58 | + } |
| 59 | + public function update2(Request $req){ |
| 60 | + $validatedData = $req->validate([ |
| 61 | + 'text' => ['required', 'string', 'max:255'], |
| 62 | + 'number' => ['required', 'integer', 'min:1'], |
| 63 | + 'image' => ['image', 'max:2048'], |
| 64 | + ], |
| 65 | + [ |
| 66 | + 'text.required' => 'Text is required', |
| 67 | + 'text.string' => 'Text must be a string', |
| 68 | + 'text.max' =>'Text must not be greater than 255 characters', |
| 69 | + 'number.required' => 'Number is required', |
| 70 | + 'number.integer' => 'Number must be an integer', |
| 71 | + 'number.min' => 'Number must be at least 1', |
| 72 | + 'image.image' => 'Image must be an image', |
| 73 | + ]); |
| 74 | + |
| 75 | + $testinput = Testinput::findorfail($req->id); |
| 76 | + $image = request()->file('image'); |
| 77 | + if($image){ |
| 78 | + $old_image=$testinput->image; |
| 79 | + if(file_exists($old_image)){ |
| 80 | + unlink($old_image); |
| 81 | + } |
| 82 | + $name = hexdec(uniqid()); |
| 83 | + $fullname = $name.'.webp'; |
| 84 | + $path = 'images/testinputs/images/'; |
| 85 | + $url = $path.$fullname; |
| 86 | + $resize_image=Image::make($image->getRealPath()); |
| 87 | + $resize_image->resize(300,300); |
| 88 | + $resize_image->save('images/testinputs/images/'.$fullname); |
| 89 | + $testinput -> text = $req -> text; |
| 90 | + $testinput -> number = $req -> number; |
| 91 | + $testinput -> image = $url; |
| 92 | + $testinput -> save(); |
| 93 | + return redirect()->route('view')->with('message','Data successfully updated'); |
| 94 | + } |
| 95 | + else{ |
| 96 | + $testinput -> text = $req -> text; |
| 97 | + $testinput -> number = $req -> number; |
| 98 | + $testinput -> save(); |
| 99 | + return redirect()->route('view')->with('message','Data successfully updated'); |
| 100 | + } |
| 101 | + } |
| 102 | + public function delete(Request $req){ |
| 103 | + $delete = Testinput::findorfail($req->id); |
| 104 | + $image = $delete->image; |
| 105 | + if(file_exists($image)){ |
| 106 | + unlink($image); |
| 107 | + } |
| 108 | + $delete->delete(); |
| 109 | + return redirect()->route('view')->with('error','Data successfully deleted'); |
| 110 | + } |
| 111 | + public function add2(){ |
| 112 | + $first_tables=Testinput::orderBy('text', 'ASC')->get(); |
| 113 | + return view('add', ['first_tables' => $first_tables]); |
| 114 | + } |
| 115 | + public function add3(Request $req){ |
| 116 | + $validatedData = $req->validate([ |
| 117 | + 'text2' => ['required', 'string', 'max:255'], |
| 118 | + 'ti_id' => ['required'], |
| 119 | + ], |
| 120 | + [ |
| 121 | + 'text2.required' => 'Text is required', |
| 122 | + 'text2.string' => 'Text must be a string', |
| 123 | + 'text2.max' =>'Text must not be greater than 255 characters', |
| 124 | + 'ti_id.required' => 'Text is required' |
| 125 | + ]); |
| 126 | + |
| 127 | + $secondtestinput = new Secondtestinput; |
| 128 | + $secondtestinput -> text2 = $req -> text2; |
| 129 | + $secondtestinput -> ti_id = $req -> ti_id; |
| 130 | + $secondtestinput->save(); |
| 131 | + return redirect()->route('add2')->with('message','Data successfully added'); |
| 132 | + } |
| 133 | + public function view2(){ |
| 134 | + $views = Secondtestinput::orderBy('text2', 'ASC')->get(); |
| 135 | + return view('view2', ['views' => $views]); |
| 136 | + } |
| 137 | + public function update3($id){ |
| 138 | + $update = Secondtestinput::findorfail($id); |
| 139 | + $first_tables = Testinput::all(); |
| 140 | + return view('update2', ['update' => $update, 'first_tables' => $first_tables]); |
| 141 | + } |
| 142 | + public function update4(Request $req){ |
| 143 | + $validatedData = $req->validate([ |
| 144 | + 'text2' => ['required', 'string', 'max:255'], |
| 145 | + 'ti_id' => ['required'], |
| 146 | + ], |
| 147 | + [ |
| 148 | + 'text2.required' => 'Text is required', |
| 149 | + 'text2.string' => 'Text must be a string', |
| 150 | + 'text2.max' =>'Text must not be greater than 255 characters', |
| 151 | + 'ti_id.required' => 'Text is required' |
| 152 | + ]); |
| 153 | + |
| 154 | + $secondtestinput = Secondtestinput::findorfail($req->id); |
| 155 | + $secondtestinput -> text2 = $req -> text2; |
| 156 | + $secondtestinput -> ti_id = $req -> ti_id; |
| 157 | + $secondtestinput -> save(); |
| 158 | + return redirect()->route('view2')->with('message','Data successfully updated'); |
| 159 | + } |
| 160 | + public function delete2(Request $req){ |
| 161 | + Secondtestinput::findorfail($req->id)->delete(); |
| 162 | + return redirect()->route('view2')->with('error','Data successfully deleted'); |
| 163 | + } |
| 164 | +} |
0 commit comments