How to use MySQL View in Laravel 9?
May 05, 2022 . Admin
Hi Guys,
Now let's see example of how to use mysql view in laravel 9 application. In this tutorial, i will show you create mysql view in laravel 9 application. I’m going to show you about laravel 9 migration create view. Here you will learn laravel 9 migration make view. i explained simply step by step how to use mysql view in laravel. Let's see bellow example laravel 9 using mysql views.
This tutorial will give you how to use mysql view in laravel 9. We will learn how to create mysql view in laravel 9.
In this example, we will create "view_user_data" view and i will get count posts and comments of that user. so we don't require to fire again and again same query on server. so let's simple example bellow:
Step 1: Download LaravelLet us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.
composer create-project laravel/laravel example-appStep 2: SQL Create View Query
CREATE VIEW view_user_data AS SELECT users.id, users.name, users.email, (SELECT count(*) FROM posts WHERE posts.user_id = users.id ) AS total_posts, (SELECT count(*) FROM comments WHERE comments.user_id = users.id ) AS total_comments FROM users;Step 3: SQL Drop View Query
DROP VIEW IF EXISTS `view_user_data`;
Now we need to use this query on our live database with laravel migration. so let's create migration with views.
Step 4: Create Migration:php artisan make:migration create_user_viewStep 5: Update Migration File:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateUserView extends Migration { /** * Run the migrations. * * @return void */ public function up() { \DB::statement($this->createView()); } /** * Reverse the migrations. * * @return void */ public function down() { \DB::statement($this->dropView()); } /** * Reverse the migrations. * * @return void */ private function createView(): string { return SQL CREATE VIEW view_user_data AS SELECT users.id, users.name, users.email, (SELECT count(*) FROM posts WHERE posts.user_id = users.id ) AS total_posts FROM users SQL; } /** * Reverse the migrations. * * @return void */ private function dropView(): string { return SQL DROP VIEW IF EXISTS `view_user_data`; SQL; } }
Now, we can run migration command:
php artisan migrate
We have created view, it's looks like as bellow:

php artisan make:model ViewUserDataapp/Models/ViewUserData.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class ViewUserData extends Model { use HasFactory; public $table = "view_user_data"; }
Now we can use it as bellow on controller file:
Step 7: Create Controllerphp artisan make:controller UserController
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\ViewUserData; class UserController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $users = ViewUserData::select("*") ->get() ->toArray(); dd($users); }Output:
array:12 [? 0 => array:4 [? "id" => 1 "name" => "Alayna Weimann" "email" => "audra.waelchi@example.org" "total_posts" => 2 ] 1 => array:4 [? "id" => 2 "name" => "Herman Mann" "email" => "selena31@example.com" "total_posts" => 1 ] 2 => array:4 [? "id" => 3 "name" => "Miss Greta Huels V" "email" => "kshlerin.alta@example.com" "total_posts" => 0 ] 3 => array:4 [? "id" => 4 "name" => "Mr. Jaeden Kunze MD" "email" => "jast.derick@example.org" "total_posts" => 0 ] 4 => array:4 [? "id" => 5 "name" => "Gerald Stoltenberg" "email" => "bashirian.earl@example.net" "total_posts" => 0 ] ]
It will help you...