Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create the migrations for database views using php artisan in Laravel?

Actually, I have managed to create a sql views for Laravel using PHP Artisan using below step.

Step 1. Run below command:

php artisan make:migration create_overall_report_views 

Step 2.

Open the migration file and add the below code:

class CreateOverallReportView extends Migration  {   /**   * Run the migrations.   *   * @return void   */   public function up()   {     //     DB::statement("       CREATE VIEW views_overall_report AS       (         SELECT er.user_id as user_id, e.id AS entities_id,              c.status_id AS status_id, s.name AS status_name          FROM `user_roles` er           LEFT JOIN elists e ON e.id=er.entities_id           LEFT JOIN `clists` c ON c.id=e.checklists_id           LEFT JOIN `status` s ON s.id = c.overall_status_id          WHERE s.slug = 'completed'           AND c.deleted_at IS NULL       )     ");   }    /**   * Reverse the migrations.   *   * @return void   */   public function down()   {     DB::statement('DROP VIEW IF EXISTS views_overall_report');   }    } 

Step 3. To call and run the SQL Views via Laravel query

$items = $DB::table('views_overall_report')             ->select('status_id', 'status_name',                 $DB::raw('count(entities_id) as counts')             )             ->groupBy('status_id')             ->orderBy('counts' , 'desc')             ->whereIn('user_id', Auth::user()->id())             ->get(); print_r($items); 

Hope that helps. Please let me know if anyone has better solution!!

like image 356
rc.adhikari Avatar asked Mar 29 '16 14:03

rc.adhikari


People also ask

How do you generate migrations in Laravel?

To create a new migration, you can run the make:migration Artisan command and that will bootstrap a new class on your Laravel application, in the database/migrations folder. This class will contain a default boilerplate code.

How does php artisan migrate work?

php artisan migrate:reset reverses all migration, unlike :rollback . php artisan migrate:fresh is used when we want a fresh or new installation of our database. It deletes all the existing tables of the database and runs the migrate command.

What is Laravel database migration?

What is Laravel Migration? Laravel Migration is an essential feature in Laravel that allows you to create a table in your database. It allows you to modify and share the application's database schema. You can modify the table by adding a new column or deleting an existing column.

Does php artisan migrate delete data?

Migrate refresh will remove all tables, then reinstall all migrations. So any data will be lost.

What is the PHP artisan migrate command in Laravel?

The php artisan migrate:installcommand will set things up for you so Laravel can manage these migrations. Over time, as your application grows, you will add more and more migrations. Laravel gives you a way to step forward and back through your migrations as needed to ensure your database is at whatever state you need it to be as you're working.

What are Laravel migrations and how to use them?

Laravel database migrations are useful for performing database operations without going deep into SQL syntax.We can create and modify tables using migrations. With the help of laravel database migrations, we can also implement version control for our database schema if we have a team of multiple developers working on same project.

How to save application links in Laravel using artisan?

In this guide, you’ll create a database migration to set up the table where you’ll save the application links. In order to do that, you’ll use the Artisan command-line tool that comes with Laravel by default. At the end, you will be able to destroy and recreate your database tables as many times as you want, using only artisan commands.

How do I migrate from one database to another using artisan?

With migrations, you just need to run php artisan migratein the command line and all changes to the database will be taken care of for you. This is basically why migrations are useful I'm not going to go into the basics of how they work because Kryten has a pretty good write-up here already.


1 Answers

Stumbled on the same issue and found the solution @ http://programmingarehard.com/2013/11/10/eloquent_and_views.html/

class CreateCompaniesView extends Migration  {     /**      * Run the migrations.      *      * @return void      */     public function up()     {         DB::statement("CREATE VIEW companiesView AS                         SELECT *,                         (                             SELECT GROUP_CONCAT(DISTINCT id SEPARATOR ',')                             FROM people AS p                             WHERE p.company_id = c.id                         ) AS person_ids                         FROM companies AS c");     }      /**      * Reverse the migrations.      *      * @return void      */     public function down()     {         DB::statement("DROP VIEW companiesView");     } }
like image 51
Peon Avatar answered Sep 17 '22 16:09

Peon