1

Is there any possible way to generate database from models in Laravel ? I am working on an existing laravel project,it contains models without their migrations.

I tried the reliese/laravel also the doctrine but nothing won't work ..any help ?

Hajer Amara
  • 29
  • 1
  • 6
  • 2
    Hi Hajer! Apologies, we may need a bit more information, what is it that you are trying to achieve? Usually you will create a model, and then a migration which will create your database tables – CodeBoyCode Mar 06 '19 at 09:39
  • @CodeBoyCode i am working at an existing laravel project .. there's no migration that contains the tables of the database ..there are only models . – Hajer Amara Mar 06 '19 at 09:45
  • Is there anything within the database folder? – CodeBoyCode Mar 06 '19 at 09:46
  • Doctrine **will** work but not with laravel models. Laravel takes a strict approach of migrations to update the database structure so if you want the doctrine approach you need to write doctrine models. Laravel models have no actual structure definition contained inside them – apokryfos Mar 06 '19 at 09:47
  • @HajerAmara, I hope, the answers of the below question may help you to proceed. https://stackoverflow.com/questions/51158766/laravel-generate-database-from-model – Ganesa Vijayakumar Mar 06 '19 at 09:47
  • @CodeBoyCode it cotains the factory , migrations and seeds. the factory contains ModelFactory , migrations contains only the two default tables create users table and create password reset table . – Hajer Amara Mar 06 '19 at 09:52
  • @GanesaVijayakumar i saw it but it did not help me :( – Hajer Amara Mar 06 '19 at 09:52
  • you may need to set up new migrations in this case - in the command line you would need to use for each of the models - php artisan make:migration create_modelname_table – CodeBoyCode Mar 06 '19 at 09:54
  • @CodeBoyCode it will create an empty table , I don't know the columns of each table that's the probleme ..how whould I know wich is the primary key or the foreign key or the names of the columns – Hajer Amara Mar 06 '19 at 09:58
  • that is true - it will be very difficult to do it without those migration files - the only thing i can think of is if they are included within the $protected_fillable array within the model - but that won't include every column – CodeBoyCode Mar 06 '19 at 10:04
  • @CodeBoyCode In my case not every model contains $protected_fillable and like you said it won't include every column also ..thx alot :) – Hajer Amara Mar 06 '19 at 10:09
  • @CodeBoyCode do you have any idea about diagram generator in Laravel ? i tried it but it didn't work . I wanted to generate the diagram so i can know the column of the tables but it didn't work – Hajer Amara Mar 06 '19 at 10:20
  • no sorry - i have never used it. It might be worth starting another question tagged with that and hopefully someone can help you with it :) – CodeBoyCode Mar 06 '19 at 10:26
  • @CodeBoyCode okay thx =D – Hajer Amara Mar 06 '19 at 10:36
  • Good luck! I hope you solve your problem – CodeBoyCode Mar 06 '19 at 10:37

1 Answers1

3

Suppose you have model Userwithout migration then you can get table name from it like

User::getTableName(); there is same function to get column names and you can get column types from protected $cast = []

Here is Sample code

        $location = new LocationTable(); // load your model
        $tableName = $location->getTable(); // this will give a table name from your model
        $columnsWithType = $location->getCasts(); // set all your table fields as cast

        Schema::create(tableName , function (Blueprint $table) use ($columnsWithType) {
            foreach ($columnsWithType as $columnName => $type) {
                $table->$type($columnName);
            }
        });
elixenide
  • 44,308
  • 16
  • 74
  • 100
Vishal Ribdiya
  • 840
  • 6
  • 18