Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

In Laravel 3 (I don't know how Laravel 4 handles it) you can choose what class to use from an alias. So if you can do this in Laravel 4, you just need to create your own class and make it extend MySqlGrammar.

Last updated 2 years ago.
0

Yes I found the solution for L3 but not for L4.

Waiting to find the solution I extended the Builder to achieve a similar result.

Last updated 2 years ago.
0
Last updated 2 years ago.
0

I've come come across the same issue. I'd like to extend the db Grammars as well as the different Connection methods (eg MySqlConnection).

thujohn, Were you able to find a solution?

Last updated 2 years ago.
0

My solution was extending the Builder and Model but no really good solution.

Last updated 2 years ago.
0

I was able to extend both the Blueprint and the MySqlGrammar classes, then implement them in my migrations using the following technique:

public function up()
{
    DB::connection()->setSchemaGrammar(new MySqlExtendedGrammar());

    $schema = DB::connection()->getSchemaBuilder();

    $schema->blueprintResolver(function($table, $callback) {
        return new ExtendedBlueprint($table, $callback);
    });

    $schema->create('table', function(ExtendedBlueprint $table) {
        // bla bla bla
    });
}
Last updated 2 years ago.
0

benharold said:

I was able to extend both the Blueprint and the MySqlGrammar classes, then implement them in my migrations using the following technique:

public function up() { DB::connection()->setSchemaGrammar(new MySqlExtendedGrammar());

   $schema = DB::connection()->getSchemaBuilder();

   $schema->blueprintResolver(function($table, $callback) {
       return new ExtendedBlueprint($table, $callback);
   });

   $schema->create('table', function(ExtendedBlueprint $table) {
       // bla bla bla
   });

}

This method appears on the face of it to work fine, however if your database has a prefix set in the database config, this method will not automatically prefix tables, so you need to remember to manually fetch the prefix using

$prefix = DB::connection()->getTablePrefix(); 

You then need to manually prepend the prefix wherever you are using your new table.

There must be an easier way to honor the prefix setting, perhaps by including this extended code somewhere else in the laravel startup, but where exactly i cannot say.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

thujohn thujohn Joined 3 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.