Convert from Kilometers to Miles Using Laravel with HTML

Hello Friends Today, through this tutorial, I will tell you Convert from Kilometers to Miles Laravel script code with html.

Here’s an example of converting Kilometers to Miles using Laravel with HTML:

Controller (app/Http/Controllers/ConverterController.php):

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ConverterController extends Controller
{
public function index()
{
return view('converter');
}
public function convert(Request $request)
{
$value = $request->input('value');
$converted = $value * 0.621371; // Conversion factor
return view('converter', compact('value', 'converted'));
}
}

Explanation:

* The `index` method returns the view named `converter`.
* The `convert` method retrieves the value from the request and performs the conversion using the multiplication factor. It then returns the view again, passing the original value and the converted value to the view.

View (resources/views/converter.blade.php):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kilometers to Miles Converter</title>
</head>
<body>
<h1>Kilometers to Miles Converter</h1>
<form method="post" action="{{ route('converter.convert') }}">
@csrf
<label for="value">Enter value in Kilometers:</label>
<input type="number" name="value" id="value" required>
<br>
<button type="submit">Convert</button>
</form>
@if (isset($converted))
<p><b>{{ $value }} Kilometers is equal to {{ $converted }} Miles.</b></p>
@endif
</body>
</html>

Explanation:

* The view displays a form with a label and input field for the value in kilometers.
* The form submits the data to the `convert` method of the `ConverterController` using the route named `converter.convert`.
* The Blade templating engine is used to display the converted value if it exists.

Routes (routes/web.php):

Route::get('/', 'ConverterController@index')->name('converter');
Route::post('/convert', 'ConverterController@convert')->name('converter.convert');

Explanation:

* This defines two routes:
* The first route maps the root path (/) to the `index` method of the `ConverterController` and assigns it the name `converter`.
* The second route maps the POST request to `/convert` to the `convert` method of the same controller and assigns it the name `converter.convert`.

Note:

1.This is a basic example. You may need to modify it based on your specific project structure and requirements.
2.Make sure to replace `{{ route(‘converter.convert’) }}` with the actual route name in your project.