Create table in database with php framework Laravel 4 - php

I am new in Laravel 4
I am trying to develop a php web application with Laravel 4
I have the following question: It is possible to create table in the database without using the terminal to run the following commands :
php artisan migrate: make create_users_table
and
php artisan migrate

the migration is a very convenient way of splitting your database into various changes, so you - or others from your team - can easily migrate to the current DB scheme. you can also seed the database with default data when using artisan migrate:refresh --seed which makes it a breeze to reset your DB content.
also it's very useful for unit tests.
surely you could create your own tables via DB::statement('create table table_name') but i strongly advise you to go the migrations route. you could also create one file for creating your initial tables instead of having to artisan migrate:make all of them.

It should be possible to use the Schema class as is used for creating migrations. So just use the following code in your controller or wherever you want.
Schema::create('create_users_table', function($table) {
$table->increments('id');
$table->string('something_else');
});
This is a nice reference for creating migrations:
http://laravelbook.com/laravel-migrations-managing-databases/

Related

Laravel Multiple Migration Table in one Statement

I am new to Laravel and I'm confused if it is possible to create a multiple migration table in Laravel in just one run?
Like:
php artisan make:migration user_acc_tbl --create=user_acc_tbl, user_info_tbl --create=user_info_tbl, skills_tbl --create=skills_tbl
I hope it is possible. So that we can just run the command once from the command terminal to create multiple migration files. It would be really helpful though.
According to the Laravel documentation, it is not possible to make multiple migrations in one command line. You may be able to write a custom artisan console command and do it that way, however I really don't see how it would save that much time.
What you could do is create multiple tables from one migration file. Just use Schema::create and create the proper tables with the necessary columns.
You could also create aliases to help speed up workflow in the terminal.

How to up a specific migration (e.g. m151125_053608) in YII 1.1

Is it possible to migrate up a specific migration in Yii 1.1 ?
I have created some migrations but i want to execute a specific migration before all other migrations are up.
Also, yiic migrate up 3 will migrate the latest three migrations and at the same time when i used yiic migrate up 1 , it doesn't asking for the intended migration.
I have tried yiic migrate up m151125_053608 but it is not working.
How to achieve such scenario? Thanks
If you already create three migrations and want to add new one to be first. Then just create file name matching that RegExp /^(m(\d{6}_\d{6})_.*?)\.php$/.
Something like this: m150101_000000_100.php
But all next migrations shoudn't be applied yet.
I suggest two ways to make this:
Run the command:
yiic migrate to 151125_053608 # In this case
By modifing the migration info (if the migration to apply is the last)
Important: copy the timestamp to restaurate the migration info later.
yiic migrate mark ######_###### Timestamp of the penultimate migration
After this do
yii migrate
Apply migration and restore the migration information with the timestamp copied.
Ref: https://www.yiiframework.com/doc/guide/1.1/en/database.migration

Creating a mysql view using php artisan for migration?

How do you create a mysql view using php artisan? Can't find any documentation on it???
Google and Bing aren't returning much at all!
documentation for Laravel: http://laravel.com/docs/migrations
Your question is a little unclear. It seems you may not understand the difference between Views, Migrations and MySQL Tables.
Migrations can be created with Artisan via php artisan make:migration ... or manually. They contain code that create MySQL Tables.
Views are template files that display a page for end users to interact with. In Laravel, Views are Blade Templates.
Artisan does not offer a command for creating Views. But there are Composer packages you can install that will extend Artisan with such functionality.
https://github.com/bencomeau/artisan-make-view
https://github.com/svenluijten/artisan-view

Laravel - Migration scripts used multple times across platforms

I just had a thought of serving fresh data for a demo app.
What I want to achieve is to have the schema and a set of data prepared beforehand and got ready to be migrated as the demo app is run. Simply put, every time a user runs the app, the database will be dumped with fresh data.
Is that possible? In Laravel 4, I know when I generate a migration script through the CLI, it seems like it has a timestamps attached to the file name, I was wondering if that would prevent the migration from being executed in later days in the future?
Thanks everyone.
What I want to achieve is to have the schema and a set of data prepared beforehand and got ready to be migrated as the demo app is run. Simply put, every time a user runs the app, the database will be dumped with fresh data.
Speaking of having a set of prepared data, I think Database Seeding is close to what you want.
The docs also mention how you could use the migration and seeding together. Although you'll need to decide how to do the migration + seed yourself. E.g. trigger by artisan command, trigger every page load, etc.
Rollback all migrations and run them all again (reference):
php artisan migrate:refresh
php artisan migrate:refresh --seed
You could also use packages like fzaninotto/Faker to help with generating a closer-to-real-life seed data for you.
In Laravel 4, I know when I generate a migration script through the CLI, it seems like it has a timestamps attached to the file name, I was wondering if that would prevent the migration from being executed in later days in the future?
The timestamp in the migration file name is only used for indicating the order in which the migration should be run. So you don't end up running migrations where you try to add a column before adding its table. As long as you do php artisan migrate:refresh and you have set up the up() and down() in your migration files correctly, it should not be a problem.
Although note that if you are in a team, be sure to check the migration order since two or more members can create migration files in parallel to each other.

Generating migration from existing database in Yii or Laravel

I'm working on a project that has a fairly complex database (150+ tables). In order to be able to maintain changes, I've decided to add migrations, preferably using Yii or Laravel.
Does anybody know, if it is possible to generate a initial migration from an existing database?
Creating it by hand would:
take for ever and
be very error-prone.
If there is no way, does anybody know a good PHP-based framework, that supports such functionality?
Instructions for accomplishing this in Yii:
Add your database connection settings to protected/config/console.php.
Run yiic migrate create initial to create the stub code for the migration.
Copy contents of this gist to protected/commands/InitialDbMigrationCommand.php.
Run yiic initialdbmigration 'name_of_your_database' > initial_migration.php to generate up() and down() methods for initial database migration.
Copy and paste up() and down() methods from initial_migration.php to the file created in the protected/migrations folder in step 2.
'Doctrine Project' (aka Doctrine) has the ability to create DB migrations for existing DB structures, so you can recreate the existing structure. It can be easily implemented in Symfony, Laravel, also in Yii and many frameworks.
Sample from:
http://symfony.com/legacy/doc/doctrine/1_2/en/07-Migrations
From Database
If you have an existing database you can build a set of migration
classes that will re-create your database by running the following
command.
$ ./symfony doctrine:generate-migrations-db
From Models
If you have an existing set of models you can build a set of migration
classes that will create your database by running the following
command.
$ ./symfony doctrine:generate-migrations-models
Here is a Laravel package I created that does exactly that. It automatically generates clean and accurate Laravel migrations from your existing database.
As it doesn't make any assumptions of the database, it should work on any database structure while even keeping the original index and foreign key names.
https://github.com/Xethron/migrations-generator
Well since migration is about setting up your database structure and make changes to it, not to reflect a current database there is no such way.
And this is also not a step you have to make. You can start from where you are at the moment, which will make you able to rollback up to this point. Which means you can make migrations for your current tables without having to specify their entire structure, but just the changes only.
Let's say you have a table called user and want to add their firstname to it.
php artisan migrate:make add_firstname_to_user
Now go into application/migrations and find the migration file, add this
public function up()
{
Schema::table('user', function($table)
{
$table->string('firstname');
});
}
public function down() {
Schema::table('user', function($table)
{
$table->drop_column('firstname');
});
}
Now you can add migrate it
php artisan migrate:install // if you haven't run this, should only be once
php artisan migrate
.. and rollback using
php artisan migrate:rollback
This will add or drop the column firstname, without affecting your table in any other way.
As for Yii 1.x, schmunk has created a wonderful database-command yiic command.
This command covers only up migrations. You must write your own down migrations.
To use it:
Get the newest version from GitHub and put it's contents into /protected/commands folder (create one, if it does not exist). Note, that you need to put contents as is (without subfolder for this particular command), which is contrary to what we do for example for extensions.
Rename EDatabaseCommand.php file (and class inside) to DatabaseCommand.php, if you want to use yiic database command (as suggested in docs). Without this fix, you'll have to use yiic edatabase command, as there's slight inconsistency between docs and the code (at least in the newest version, as of writing this; maybe schmunk is going to fix this).
Having this, navigate back to protected folder in your console and execute yiic database dump migration_name --prefix=table_name.
This will create a migration protected/runtime/migration_name.php file with proper date and time in the beginning of file name, filled with series of CDbMigration commands to recreate your database schema. Visit "Usage" section in the docs to read more about customizing command.
I think that the answer is: https://github.com/jamband/yii2-schemadump for Yii2
"This command to generate the schema from an existing database."
I use both Yii and Laravel and I could not find what you require for either of them. They both create empty files and you need to create the migration script yourself.
For a table of 150 tables it will be challenge to create the migrations yourself, but it is not quite as hard as you imagine. Because you already have the information on the fields it should not take so long to create.
After doing some research, here's what you're going to need for Laravel: https://github.com/XCMer/larry-four-generator
(version 4 at least, who knows how long this will work, Laravel changes too fast and has too many breaking changes)
You'll want to run php artisan larry:fromdb and it'll show you the tables...You can also exclude or only process certain tables (look at the readme).
Again, super super useful if you like to build your schema in something like MySQL Workbench. I also saw mention of a package that would parse the workbench files...But the link was dead.
You may also wish to use this larry package with: https://github.com/JeffreyWay/Laravel-4-Generators
You can then create scaffolding a la CakePHP style.
Alternatively, try this package: https://github.com/barryvdh/laravel-migration-generator
There is one now for Yii:
This allows a distributed team to easily update the db locally and then distribute it's updates with thee other developers automatically with the rest of the code via a versioning control system (I used git). It also performs a full initial db dump to xml and to a migration file.
project home:
https://code.google.com/p/yii-automatically-generated-migration-files/
source code:
https://code.google.com/p/yii-automatically-generated-migration-files/source/checkout
I've created it from scratch as I was annoyed with the fact that I had to do this manually in order to distribute it to my team.
Hope it helps!
Feel free to share bugs, improvements and comments.

Categories