Midterm Exam Laboratory
Midterm Exam Laboratory
(MVC) architecture within the Laravel framework to build a fully functional transaction-based ordering
system. Students will learn how to create models, views, and controllers, as well as define routes,
manage database migrations, and implement data validation and user interface improvements.
Instructions:
First, ensure you have Laravel installed. If not, you can create a new Laravel project by running:
cd order-system
Next, create a model for the order and the corresponding database migration:
This command creates an Order model and a migration file in the database/migrations directory.
Open the migration file located in database/migrations, which will be named something like
xxxx_xx_xx_xxxxxx_create_orders_table.php. Update it to include the necessary fields:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
$table->id();
$table->string('product_name');
$table->integer('quantity');
$table->decimal('price', 8, 2);
$table->timestamps();
});
Schema::dropIfExists('orders');
Open the newly created OrderController.php in app/Http/Controllers and add the following methods:
namespace App\Http\Controllers;
use App\Models\Order;
use Illuminate\Http\Request;
$orders = Order::all();
return view('orders.create');
$request->validate([
]);
Order::create($request->all());
$order->delete();
}
Step 5: Define Routes
Open the routes/web.php file and define the routes for the ordering system:
use App\Http\Controllers\OrderController;
Route::resource('orders', OrderController::class);
mkdir resources/views/orders
<!DOCTYPE html>
<html>
<head>
<title>Orders</title>
</head>
<body>
<h1>Orders</h1>
@if(session('success'))
@endif
<table border="1">
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Actions</th>
</tr>
<tr>
<td>
@csrf
@method('DELETE')
<button type="submit">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Create Order</title>
</head>
<body>
<h1>Create Order</h1>
@if ($errors->any())
<div>
<ul>
@endforeach
</ul>
</div>
@endif
@csrf
<div>
</div>
<div>
<label for="quantity">Quantity:</label>
</div>
<div>
<label for="price">Price:</label>
</div>
</body>
</html>
Open the Order.php model located at app/Models/Order.php and add the following code to allow
mass assignment:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use HasFactory;
Now that you have set up the basic components of your ordering system, you can test it. Start the
Laravel development server:
This design will enhance the user interface of the ordering system you created in Laravel. You can apply
this design to your index.blade.php and create.blade.php views.
CSS Styles
You can create a separate CSS file for styles. Let's create a file named styles.css in the public/css
directory.
mkdir public/css
touch public/css/styles.css
body {
background-color: #f4f4f4;
margin: 0;
padding: 20px;
h1 {
color: #333;
.container {
max-width: 800px;
margin: auto;
background: white;
padding: 20px;
border-radius: 5px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
table, th, td {
th, td {
padding: 10px;
text-align: left;
th {
background-color: #f2f2f2;
button {
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
}
button:hover {
background-color: #45a049;
a{
text-decoration: none;
color: #007BFF;
a:hover {
text-decoration: underline;
.error {
color: red;
margin-bottom: 20px;
form div {
margin-bottom: 15px;
let's update the index.blade.php and create.blade.php views to use the new styles.
index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Orders</title>
</head>
<body>
<div class="container">
<h1>Orders</h1>
@if(session('success'))
@endif
<table>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Actions</th>
</tr>
<tr>
<td>
@csrf
@method('DELETE')
<button type="submit">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
</div>
</body>
</html>
create.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Order</title>
</head>
<body>
<div class="container">
<h1>Create Order</h1>
@if ($errors->any())
<div class="error">
<ul>
@endforeach
</ul>
</div>
@endif
@csrf
<div>
</div>
<div>
<label for="quantity">
<label for="quantity">Quantity:</label>
</div>
<div>
<label for="price">Price:</label>
</div>
</form>
</div>
</body>
</html>
Best of luck with your Laravel project!
Trust your coding skills, stay focused, and
remember: every bug is just a step closer to
success.
– Ma’am K