Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend Blueprint class?

Tags:

I want to override the timestamps() function found in the Blueprint class. How can I do that?

e.g.,

public function up() {     Schema::create('users', function (Blueprint $table) {         $table->increments('id');         $table->string('username')->unique();         $table->string('password');         $table->string('email');         $table->string('name');         $table->timestamps(); // <-- I want this to call my method instead of the one found in the base Blueprint class     }); } 
like image 633
mpen Avatar asked Mar 16 '14 23:03

mpen


People also ask

How to add a Blueprint ue5?

Using the Add New ButtonIn the Content Browser, click the Add New button. Select Blueprint Class from the Create Basic Asset section of the dropdown menu. You can create several different types of Blueprint Assets from the Blueprints option under Create Advanced Asset. Choose a Parent Class for your Blueprint Asset.

What is Blueprint class?

Definition: A class is a blueprint that defines the variables and the methods common to all objects of a certain kind.

What is Blueprint in C++?

C++ classes can be extended with Blueprints, allowing programmers to set up new gameplay classes in code that can be built upon and changed with Blueprints by level designers.

What is Blueprint in laravel?

Blueprint is an open-source tool for rapidly generating multiple Laravel components from a single, human-readable definition. Blueprint works by using a draft file (YAML) that contains definitions of the components it should generate.


1 Answers

There is a new blueprintResolver function which takes a callback function which then returns the Blueprint instance.

So create your custom Blueprint class like this:

class CustomBlueprint extends Illuminate\Database\Schema\Blueprint{      public function timestamps() {         //Your custom timestamp code. Any output is not shown on the console so don't expect anything     } } 

And then call the blueprintResolver function where you return your CustomBlueprint instance.

public function up() {     $schema = DB::connection()->getSchemaBuilder();      $schema->blueprintResolver(function($table, $callback) {         return new CustomBlueprint($table, $callback);     });      $schema->create('users', function($table) {         //Call your custom functions     }); } 

I'm not sure if creating a new schema instance with DB::connection()->getSchemaBuilder(); is state of the art but it works.

You could additionally override the Schema facade and add the custom blueprint by default.

like image 141
Marcel Gwerder Avatar answered Sep 21 '22 16:09

Marcel Gwerder