ItSolutionStuff.com

How to Use MySQL View in Laravel?

By Hardik Savani • April 16, 2024
Laravel

In this short tutorial we will cover an how to call mysql view in laravel. I’m going to show you about laravel migration create view. Here you will learn laravel migration make view. i explained simply step by step how to use mysql view in laravel. Let's see bellow example laravel using mysql views.

You can create mysql view using migration in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

In this post, i will show you how to create mysql view using laravel migration and how to use mysql view with laravel eloquent model.

basically, we are using sql view because we don't require to write long query on our database again and again. if you create simple view then you can easily get data from that view.

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:

Bellow my sql query for creating view and drop view:

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

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.

Create Migration:

php artisan make:migration create_user_view

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 <<

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

SQL;

}

/**

* Reverse the migrations.

*

* @return void

*/

private function dropView(): string

{

return <<

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:

now we will create model as bellow:

app/ViewUserData.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ViewUserData extends Model

{

public $table = "view_user_data";

}

Now we can use it as bellow on controller file:

Controller file code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\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);

}

}

you can see output as bellow:

array:203 [▼

0 => array:5 [▼

"id" => 1

"name" => "Prof. Josiane Jast MD"

"email" => "savanihd@gmail.com"

"total_posts" => 3

"total_comments" => 2

]

1 => array:5 [▼

"id" => 2

"name" => "Mr. Donavon Beer"

"email" => "yemmerich@example.net"

"total_posts" => 0

"total_comments" => 0

]

2 => array:5 [▼

"id" => 3

"name" => "Judy Watsica"

"email" => "gutmann.christian@example.org"

"total_posts" => 0

"total_comments" => 0

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Eloquent whereNotNull() Query Example

Read Now →

Laravel Migration - How to Add New Column in Existing Table ?

Read Now →

How to Change Table Name using Laravel Migration?

Read Now →

How to Remove Column from Table in Laravel Migration?

Read Now →

How to Change Column Name and Data Type in Laravel Migration?

Read Now →

How to Create Table using Migration in Laravel?

Read Now →

How to Call Model Function from Another Model in Codeigniter?

Read Now →

Laravel Many to Many Eloquent Relationship Tutorial

Read Now →

Laravel Improve Speed Performance using Model Caching Tutorial

Read Now →

Laravel Model Disable created_at and updated_at Update Record

Read Now →

How to Drop Foreign Key Constraint in Laravel Migration?

Read Now →

How to Add MySQL Trigger from Migration in Laravel?

Read Now →