Sure! Here is an example of a homepage in Laravel framework that allows users to calculate how to share a total cost based on the number of people and their invoices.
First, make sure you have Laravel and PHP installed on your system. Then, follow these steps:
Step 1: Create a new Laravel project
Open your command line interface and navigate to the desired directory. Run the following command to create a new Laravel project:
$ composer create-project --prefer-dist laravel/laravel cost-sharing
Step 2: Create routes and views
Next, open the routes/web.php file and define a route to handle the homepage:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'index'])->name('home');
Route::post('/calculate', [HomeController::class, 'calculate'])->name('calculate');
Now, create a new controller named HomeController by running the following command:
$ php artisan make:controller HomeController
Open the app/Http/Controllers/HomeController.php file and add the following code:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index()
{
return view('home');
}
public function calculate(Request $request)
{
$people = $request->input('people');
$totalCost = $request->input('totalCost');
$invoices = $request->input('invoices');
// Perform calculation here
// Example calculation: evenly distribute total cost among people
$share = round($totalCost / $people, 2);
return view('result', compact('share'));
}
}
Now, create two new views. First, create resources/views/home.blade.php:
<!DOCTYPE html>
<html>
<head>
<title>Cost Sharing Calculator</title>
</head>
<body>
<h1>Cost Sharing Calculator</h1>
<form method="POST" action="{{ route('calculate') }}">
@csrf
<label for="people">Number of People:</label>
<input type="number" name="people" required><br><br>
<label for="totalCost">Total Cost:</label>
<input type="number" name="totalCost" required><br><br>
<label for="invoices">Invoices (comma-separated):</label>
<input type="text" name="invoices" required><br><br>
<button type="submit">Calculate</button>
</form>
</body>
</html>
Next, create resources/views/result.blade.php:
<!DOCTYPE html>
<html>
<head>
<title>Cost Sharing Result</title>
</head>
<body>
<h1>Cost Sharing Result</h1>
<p>Each person should pay: ${{ $share }}</p>
</body>
</html>
Step 3: Run the Laravel development server
Open your command line again, navigate to the project directory, and run the following command to start the Laravel development server:
$ php artisan serve
Step 4: Test the application
Open your web browser and visit http://localhost:8000. You should see the homepage with the input form. Enter the number of people, total cost, and a list of invoices separated by commas. Upon submitting the form, you will be redirected to the result page, displaying the calculated share for each person.
Note: This is a basic example to demonstrate the concept. You can modify the calculation logic and add further validation as per your requirements.