How to Use MySQL View in Laravel
How to Use MySQL View in Laravel

How to Use MySQL View in Laravel

MySQL Views can be very useful in your Laravel projects. A View is a virtual table based on the result set of a SQL query. In Laravel, using MySQL Views can simplify complex queries and improve performance by allowing you to reuse SQL code.

This article will guide you through the steps to create and use MySQL Views in a Laravel application.

Step 1: Create a MySQL View

First, we need to create a MySQL View. You can do this directly in your MySQL database. For example, let’s say we have a table called orders and another table called customers. We want a View that shows customer names and their total orders.

CREATE VIEW customer_orders AS
SELECT AS customer_name,
    COUNT( AS total_orders
    orders ON = orders.customer_id

This SQL query creates a View named customer_orders that displays the customer name and the total number of orders.

Step 2: Configure Laravel to Use the View

In Laravel, you need to set up a model to interact with the View. Let’s create a model for the customer_orders View.

  1. Create the Model:
    Use the Artisan command to generate a model:
   php artisan make:model CustomerOrder
  1. Define the Model:
    Open the generated CustomerOrder.php model file in the app/Models directory and ensure it looks like this:

   namespace App\Models;

   use Illuminate\Database\Eloquent\Model;

   class CustomerOrder extends Model
       // Specify the table name
       protected $table = 'customer_orders';

       // Disable timestamps for the view
       public $timestamps = false;

Step 3: Use the View in Your Controller

Now, you can use the CustomerOrder model in your controllers to fetch data from the customer_orders View.

  1. Create a Controller:
    Generate a new controller using Artisan:
   php artisan make:controller CustomerOrderController
  1. Fetch Data in the Controller:
    Open the CustomerOrderController.php file in the app/Http/Controllers directory and add the following code to fetch data from the View:

   namespace App\Http\Controllers;

   use App\Models\CustomerOrder;
   use Illuminate\Http\Request;

   class CustomerOrderController extends Controller
       public function index()
           // Fetch data from the customer_orders view
           $customerOrders = CustomerOrder::all();

           // Return the data to a view
           return view('customer_orders.index', ['customerOrders' => $customerOrders]);

Step 4: Create a View File to Display Data

Finally, create a Blade view file to display the data fetched from the MySQL View.

  1. Create the View Directory:
    Inside the resources/views directory, create a new directory named customer_orders.
  2. Create the Index View:
    Inside the customer_orders directory, create a file named index.blade.php and add the following code:
   <!DOCTYPE html>
   <html lang="en">
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>Customer Orders</title>
       <h1>Customer Orders</h1>
       <table border="1">
                   <th>Customer Name</th>
                   <th>Total Orders</th>
               @foreach ($customerOrders as $order)
                       <td>{{ $order->customer_name }}</td>
                       <td>{{ $order->total_orders }}</td>

Step 5: Define a Route

To access the data, you need to define a route in the routes/web.php file:

use App\Http\Controllers\CustomerOrderController;

Route::get('/customer-orders', [CustomerOrderController::class, 'index']);

Testing Your Setup

Now, navigate to http://your-laravel-app.test/customer-orders in your browser. You should see a table displaying customer names and their total orders.


Using MySQL Views in Laravel is a powerful way to simplify complex queries and enhance your application’s performance. By following the steps above, you can easily create and use MySQL Views in your Laravel projects. This approach helps in managing and displaying data more efficiently, ensuring your code remains clean and maintainable.