Pārlūkot izejas kodu

done enquiry api

master
azri 1 mēnesi atpakaļ
vecāks
revīzija
8272246cf5

+ 78
- 0
app/Http/Controllers/EnquiryController.php Parādīt failu

@@ -0,0 +1,78 @@
1
+<?php
2
+
3
+namespace App\Http\Controllers;
4
+
5
+use App\Models\Enquiry;
6
+use Illuminate\Http\Request;
7
+use Illuminate\Support\Facades\Validator;
8
+
9
+class EnquiryController extends Controller
10
+{
11
+    /**
12
+     * Display a listing of the resource.
13
+     */
14
+    public function index()
15
+    {
16
+        return "ENQUIRY API IS WORKING";
17
+    }
18
+
19
+    /**
20
+     * Store a newly created resource in storage.
21
+     */
22
+    public function store(Request $request)
23
+    {
24
+
25
+        $messages = [
26
+            'name.required' => 'The name field is required.',
27
+            'email.required' => 'The email field is required.',
28
+            'email.email' => 'Please enter a valid email address.',
29
+            'phone.required' => 'The phone number field is required.',
30
+            'phone.regex' => 'The phone number format is invalid. It should contain 10 to 15 digits.',
31
+        ];
32
+
33
+        $validatedData = $request->validate([
34
+            'name' => 'required|string|max:255',
35
+            'email' => 'required|email|max:255',
36
+            'phone' => 'required|regex:/^[0-9]{10,15}$/',
37
+        ], $messages);
38
+
39
+        if($validatedData){
40
+
41
+            Enquiry::create([
42
+                'name' => $request->name,
43
+                'email' => $request->email,
44
+                'phone' => $request->phone
45
+            ]);
46
+
47
+            return response()->json(['message' => 'ok'], 200);
48
+        }
49
+
50
+
51
+        return response()->json(['message' => 'error'], 400);
52
+
53
+    }
54
+
55
+    /**
56
+     * Display the specified resource.
57
+     */
58
+    public function show(string $id)
59
+    {
60
+        //
61
+    }
62
+
63
+    /**
64
+     * Update the specified resource in storage.
65
+     */
66
+    public function update(Request $request, string $id)
67
+    {
68
+        //
69
+    }
70
+
71
+    /**
72
+     * Remove the specified resource from storage.
73
+     */
74
+    public function destroy(string $id)
75
+    {
76
+        //
77
+    }
78
+}

+ 48
- 0
app/Http/Controllers/enquiry.php Parādīt failu

@@ -0,0 +1,48 @@
1
+<?php
2
+
3
+namespace App\Http\Controllers;
4
+
5
+use Illuminate\Http\Request;
6
+
7
+class enquiry extends Controller
8
+{
9
+    /**
10
+     * Display a listing of the resource.
11
+     */
12
+    public function index()
13
+    {
14
+        //
15
+    }
16
+
17
+    /**
18
+     * Store a newly created resource in storage.
19
+     */
20
+    public function store(Request $request)
21
+    {
22
+        //
23
+    }
24
+
25
+    /**
26
+     * Display the specified resource.
27
+     */
28
+    public function show(string $id)
29
+    {
30
+        //
31
+    }
32
+
33
+    /**
34
+     * Update the specified resource in storage.
35
+     */
36
+    public function update(Request $request, string $id)
37
+    {
38
+        //
39
+    }
40
+
41
+    /**
42
+     * Remove the specified resource from storage.
43
+     */
44
+    public function destroy(string $id)
45
+    {
46
+        //
47
+    }
48
+}

+ 16
- 0
app/Models/Enquiry.php Parādīt failu

@@ -0,0 +1,16 @@
1
+<?php
2
+
3
+namespace App\Models;
4
+
5
+use Illuminate\Database\Eloquent\Factories\HasFactory;
6
+use Illuminate\Database\Eloquent\Model;
7
+
8
+class Enquiry extends Model
9
+{
10
+    use HasFactory;
11
+
12
+    protected $table = "enquirys";
13
+
14
+    protected $fillable = ['name','email','phone'];
15
+
16
+}

+ 30
- 0
database/migrations/2024_10_09_103426_enquiry.php Parādīt failu

@@ -0,0 +1,30 @@
1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\Schema;
6
+
7
+return new class extends Migration
8
+{
9
+    /**
10
+     * Run the migrations.
11
+     */
12
+    public function up(): void
13
+    {
14
+        Schema::create('enquirys', function (Blueprint $table){
15
+            $table->id();
16
+            $table->string('name');
17
+            $table->string('email');
18
+            $table->string('phone');
19
+            $table->timestamps();
20
+        });
21
+    }
22
+
23
+    /**
24
+     * Reverse the migrations.
25
+     */
26
+    public function down(): void
27
+    {
28
+        Schema::dropIfExists('enquirys');
29
+    }
30
+};

+ 43
- 1
resources/views/home.antlers.html Parādīt failu

@@ -157,7 +157,7 @@
157 157
                 <div class="mx-auto mb-5" style="width: 70%;">
158 158
                     <h4 class="fw-bold">Hubungi kami untuk mengetahui lebih lanjut :</h4>
159 159
                 </div>
160
-                <form class="mx-auto" style="width: 70%;">
160
+                <form id="enquiry-form" class="mx-auto" style="width: 70%;">
161 161
                   <div class="form-group">
162 162
                     <label for="name">Nama Perniagaan :</label>
163 163
                     <input type="text" id="name" name="name" required>
@@ -205,3 +205,45 @@
205 205
         </div>
206 206
     </div>
207 207
 </div>
208
+
209
+<script>
210
+    let enquiryForm = document.getElementById("enquiry-form")
211
+
212
+      enquiryForm.addEventListener('submit', async function (e) {
213
+      e.preventDefault(); // Prevent form from submitting the traditional way
214
+
215
+      // Gather form data
216
+      const formData = {
217
+        name: document.getElementById('name').value,
218
+        email: document.getElementById('email').value,
219
+        phone: document.getElementById('phone').value
220
+      };
221
+
222
+      try {
223
+        // Send POST request
224
+        const response = await fetch('/api/enquiry/', {
225
+          method: 'POST',
226
+          headers: {
227
+            'Content-Type': 'application/json'
228
+          },
229
+          body: JSON.stringify(formData) // Convert form data to JSON
230
+        });
231
+
232
+        // Parse JSON response
233
+        const result = await response.json();
234
+
235
+        // Handle the response
236
+        if (response.ok) {
237
+          console.log('Enquiry submitted successfully:', result);
238
+          alert('Your enquiry has been submitted!');
239
+        } else {
240
+          console.error('Submission failed:', result);
241
+          alert('Submission failed. Please try again.');
242
+        }
243
+      } catch (error) {
244
+        console.error('Error occurred:', error);
245
+        alert('An error occurred while submitting your enquiry.');
246
+      }
247
+    });
248
+
249
+</script>

+ 3
- 0
routes/api.php Parādīt failu

@@ -1,5 +1,6 @@
1 1
 <?php
2 2
 
3
+use App\Http\Controllers\EnquiryController;
3 4
 use Illuminate\Http\Request;
4 5
 use Illuminate\Support\Facades\Route;
5 6
 
@@ -17,3 +18,5 @@ use Illuminate\Support\Facades\Route;
17 18
 // Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
18 19
 //     return $request->user();
19 20
 // });
21
+
22
+Route::resource('/enquiry', EnquiryController::class);

Notiek ielāde…
Atcelt
Saglabāt