use App\Http\Controllers\PostController;
Route::post('/submit-data', [PostController::class, 'store']);
php artisan make:controller PostController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function store(Request $request)
{
// Validate incoming request
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email',
'message' => 'required|string',
]);
// Save the data (example)
// Message::create($validatedData);
return response()->json([
'success' => true,
'message' => 'Data successfully submitted!',
'data' => $validatedData,
], 201);
}
}
axios.post('/submit-data', {
name: 'John Doe',
email: 'john.doe@example.com',
message: 'Hello, this is a test message.',
})
.then(response => {
console.log('Success:', response.data);
})
.catch(error => {
console.error('Error:', error.response.data);
});
In Postman:
{
"name": "John Doe",
"email": "john.doe@example.com",
"message": "This is a test message."
}
{
"success": true,
"message": "Data successfully submitted!",
"data": {
"name": "John Doe",
"email": "john.doe@example.com",
"message": "This is a test message."
}
}