Skip to content Skip to sidebar Skip to footer

Laravel 11 Build Multi Restaurant Food Order Application A-Z

Laravel 11 Build Multi Restaurant Food Order Application A-Z

Building a multi-restaurant food ordering application with Laravel 11 can be an exciting project that blends various web development practices such as authentication, order management, real-time notifications, and more.

Enroll Now 

This tutorial will guide you through setting up the application from scratch, covering essential features like restaurant management, customer registration, ordering, and more.

Prerequisites

Before diving into the project, ensure you have the following:

  • PHP (preferably version 8.x or higher)
  • Composer (for managing dependencies)
  • Laravel 11 (for building the application)
  • A local or remote database (MySQL is preferred)
  • Basic knowledge of Laravel, Vue.js (optional), REST APIs, and front-end development

Step 1: Setting Up Laravel 11

First, create a new Laravel project for your application by running the following command:

bash
composer create-project --prefer-dist laravel/laravel multi-restaurant-app

This will install Laravel 11 and set up the base structure for the application. Once done, you can navigate into the project folder:

bash
cd multi-restaurant-app

Ensure your .env file is set up with the correct database connection settings:

bash
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=multi_restaurant DB_USERNAME=root DB_PASSWORD=

Run the following command to generate an application key:

bash
php artisan key:generate

Then, run your migration files:

bash
php artisan migrate

Now, you have a fresh Laravel installation ready for development.


Step 2: Building the Database Schema

A multi-restaurant food ordering app requires several tables to manage restaurants, menus, orders, users, and more. Here’s a basic structure you might consider:

  • Users: Stores user information (customers, restaurant owners, admins)
  • Restaurants: Stores restaurant data (name, address, working hours, etc.)
  • Menus: Stores food items offered by restaurants
  • Orders: Stores customer orders
  • Order_Items: Stores individual items within an order
  • Payments: Stores payment details for each order

Let’s create the migration files:

bash
php artisan make:migration create_restaurants_table php artisan make:migration create_menus_table php artisan make:migration create_orders_table php artisan make:migration create_order_items_table php artisan make:migration create_payments_table

In create_restaurants_table.php, you might define the schema as follows:

php
Schema::create('restaurants', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('address'); $table->string('phone_number'); $table->text('description')->nullable(); $table->string('working_hours'); $table->timestamps(); });

Similarly, you would define other tables such as menus, orders, order_items, and payments according to the data they need to store.

After defining the migrations, run them:

bash
php artisan migrate

Step 3: Setting Up Authentication and Roles

Laravel comes with built-in authentication scaffolding, which you can install by running:

bash
composer require laravel/ui php artisan ui vue --auth npm install && npm run dev

Now, Laravel will provide you with ready-made login and registration routes. However, for a multi-restaurant app, we need to differentiate between customers, restaurant owners, and admins. This can be achieved by adding a role field to the users' table.

Modify the users table migration to add a role column:

php
$table->enum('role', ['customer', 'restaurant_owner', 'admin'])->default('customer');

Now, using middleware, you can restrict access to certain pages based on the role:

php
php artisan make:middleware CheckRole

In CheckRole.php, write logic to check the role of the authenticated user:

php
public function handle($request, Closure $next, $role) { if (auth()->user()->role !== $role) { return redirect('/'); } return $next($request); }

Register this middleware in app/Http/Kernel.php:

php
'role' => \App\Http\Middleware\CheckRole::class,

Now, protect routes by using middleware in routes/web.php:

php
Route::group(['middleware' => ['auth', 'role:restaurant_owner']], function () { Route::get('/restaurant/dashboard', [RestaurantController::class, 'dashboard']); });

Step 4: Restaurant Management

For restaurant owners, they should be able to add their restaurant details, menus, and manage orders. Create a RestaurantController for handling restaurant-related operations:

bash
php artisan make:controller RestaurantController

In RestaurantController.php, add methods for CRUD operations on restaurants:

php
public function create() { return view('restaurants.create'); } public function store(Request $request) { $request->validate([ 'name' => 'required', 'address' => 'required', 'phone_number' => 'required', ]); Restaurant::create($request->all()); return redirect()->route('restaurant.dashboard'); }

For menus, create a similar controller, and ensure restaurant owners can manage their food items:

bash
php artisan make:controller MenuController

Step 5: Customer Registration and Ordering

For customers, they need to browse restaurants, view menus, and place orders. In your HomeController, you might fetch a list of restaurants and pass them to the view:

php
$restaurants = Restaurant::all(); return view('home', compact('restaurants'));

In the restaurant view, display the list of menus, allowing customers to add items to their order.

Create an OrderController to handle order placement:

php
php artisan make:controller OrderController

In the store method of OrderController, create logic to store orders and their items:

php
public function store(Request $request) { $order = Order::create([ 'user_id' => auth()->id(), 'restaurant_id' => $request->restaurant_id, 'total' => $request->total, ]); foreach ($request->items as $item) { OrderItem::create([ 'order_id' => $order->id, 'menu_id' => $item['menu_id'], 'quantity' => $item['quantity'], ]); } return redirect()->route('order.success'); }

Step 6: Real-Time Order Notifications

Using Laravel Echo and Pusher (or another WebSocket service), you can provide real-time notifications to restaurant owners when a new order is placed. Install Laravel Echo and Pusher:

bash
composer require pusher/pusher-php-server npm install --save laravel-echo pusher-js

Configure broadcasting.php and .env with Pusher settings, then create an event for when an order is placed:

bash
php artisan make:event OrderPlaced

Fire this event in the store method of OrderController:

php
OrderPlaced::dispatch($order);

Listen for this event on the front-end using Laravel Echo to notify restaurant owners:

js
Echo.channel('orders') .listen('OrderPlaced', (e) => { console.log(e.order); });

Step 7: Payment Integration

For payments, integrate a third-party service such as Stripe or PayPal. Install the Stripe PHP package:

bash
composer require stripe/stripe-php

In your PaymentController, handle payment processing:

php
use Stripe\Stripe; use Stripe\Charge; public function process(Request $request) { Stripe::setApiKey(env('STRIPE_SECRET')); Charge::create([ 'amount' => $request->amount * 100, 'currency' => 'usd', 'source' => $request->stripeToken, ]); // Save payment details to database }

Create a payment form and route it through the controller to process transactions.


Step 8: Deployment and Optimization

Finally, when the app is complete, prepare it for deployment by optimizing it for performance and security:

bash
php artisan optimize php artisan config:cache php artisan route:cache

You can deploy the app to a server or a cloud service like DigitalOcean or AWS, ensuring that you have set up SSL certificates and optimized the database queries for a better user experience.


Conclusion

Building a multi-restaurant food ordering application with Laravel 11 involves setting up a robust architecture with authentication, role management, restaurant and menu handling, real-time notifications, and payment integration. By following the steps outlined in this guide, you can create a fully functional application ready for deployment.

React JS: Build 6 Real-World React Apps with AI Integration Udemy

Post a Comment for "Laravel 11 Build Multi Restaurant Food Order Application A-Z"