Create View in Mysql through Laravel

Asked

Viewed 1,308 times

6

How can I create a view in the Mysql database through migrations of Laravel? I found nothing in the documentation.

  • Laravel Migrations works as an ORM?

  • Yes, it works..

  • do not create views in the database, if your Framework provides an option to do so. see here an example of that in the application.

  • Any specific reason not to create Views? What’s the downside?

  • Maintenance, you wouldn’t need to tinker with the database only in the application.

1 answer

4


There is a yes and viable way to create Views with Database Migrations, but, it’s a forma textual:

Commando: DB::statement, in place of Schema::create

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Creditosativos extends Migration
{
    public function up()
    {
        DB::statement("CREATE VIEW v_creditos_ativos AS
                        SELECT * from creditos where status = 1");
    }
    public function down()
    {
        DB::statement("DROP VIEW v_creditos_ativos");
    }
}

Reference:

  • 2

    Thank you very much!

  • 1

    No reason @Amandalima, it’s nice to have someone from the Laravel here ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.