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!!
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.
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 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.
Migrate refresh will remove all tables, then reinstall all migrations. So any data will be lost.
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.
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.
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.
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.
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"); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With