i have an existing database and all my models are defined already. I would like to scaffold Controllers and Views because it's really time consuming.
i found this which sound good: infyom
They say in the documentation that you can use it from an existing database but any command keeps asking me for fields, (as it is trying to create a model from scratch), what i want to to use my existing models.
I found the "scaffold from table" option in the documentation:
php artisan infyom:scaffold Equipement --fromTable --tableName=Equipement
But it says:[ErrorException]
Undefined index: Equipement
i guess it did not find my existing model.
I was wondering, what do they call datatable anyway ? I can see in their config file they have a folder for that, maybe i should define my models there somehow ?
Thanks for anyone who can help on that. (or providing an alternative solution for scaffolding from existing database/models)
You must add id and:
updated_at` TIMESTAMP NULL DEFAULT NULL,
deleted_at` TIMESTAMP NULL DEFAULT NULL
in your table.
Related
Can anyone help me to automatically save the history of all changed columns in a model to a history column in the same table as JSON format with column(s) name, original value, changed to, changed by?
I am looking for Traits like centralized function to use in all models.
I am using Laravel 8 with PostgreSQL.
This answer is maybe outdated since it is Laravel 4. But if it works, it is quickest way to insert log in DB wherever save() method is called for that model.
https://stackoverflow.com/a/20694395/13893004
I found many results how to create migration from a model, but is there any way to create a model from migrations?
Basically I want to give a table name, and if there is multiple migration files (one to create and others to update the previous states) the model could be updated.
Is there any solution for this?
I'm not sure that there's a good way to create a model directly from migrations, as it depends on what database you use as to the exact structure of your tables.
There seems to be a package which support generating the models from your database, however:
https://github.com/laracademy/generators
I have some tables that are already created in the database. The migrations seem to require that you first destroy the table and then create it. From what I understand, the creation process generates a "model" at the same time as well as "getters" and "setters" for the fields.
The problem is that I have a database full of tables already. I would like to access the tables that have already been created using Laravel as an interface. Is there a way to generate a model, getters and setters for tables that already exist?
How can I best do this?
TIA
The default model generator doesn't create any getters or setters. All it does is create a basic model class with the correct model name based on the table name. You can create the models without actually creating the migration. The command is php artisan make:model ModelName.
So the answer to your question is no, there isn't any. You need to manually create each model and add the methods yourself. You might try your luck with some laravel package which might do this for you.
Note : Laravel Eloquent is an ActiveRecord implementation. You don't need any getters or setters to work with the table columns. Even a completely empty model with the correct name to match the table would be enough to start using Eloquent.
I am interested in how to do MySQL column name suggestion.
Here is my simple database.
This is my php code in route/web.php
This is my about.blade.php file
I want it to suggest me MySQL column name like id, body, created_as, update_at.. like this $task->... and here to suggest me all the column names which has table.
Is this possible?
Sort of.
However, you wouldn't be able to use the DB class, you'd need to create a Model and create the appropriate #property tags in the docblock.
Sometimes it works, sometimes it doesn't I have noticed. It really depends if PHPStorm picks up the proper context. It likely won't be picked up automatically in blade templates but you can type hint any variable:
/** #var TaskModel $task */
There is a good plugin for IDE autocompletion with regards to Laravel here: https://github.com/barryvdh/laravel-ide-helper
I've configured my database.yml to point to my existing mysql database
how can I generate models from it?
rails generate model existing_table_name
only gives an emty model..
You can try Rmre. It can create models for existing schema and it tries to create all relationships based on foreign keys information.
A Rails model doesn't show your fields, but you can still use them. Try the following. Assuming you have a Model named ModelName and a field called "name", fire up the Rails console and type:
ModelName.find_by_name('foo')
Given a name that exists in the DB, you should see results.
Rails doesn't infer relationships though, but if your database follows Rails conventions they are easily added.
Update
I've noticed this particular lack of explicitness ("magic") is a source of confusion for newbies to Rails. You can always look in schema.rb to see the models and all the fields in one place. Also, if you would prefer to see the schema for each model in the model file, you can use the annotate_models gem, which will put the db schema in a comment at the top of the model file.
Your answer is:
$ rake db:schema:dump
That will set a new db/schema.db to create a schema of your DB.
ActiveRecord doesn't parse a schema definition. It asks the DBM for the table defs and figures out the fields on the fly.
Having the schema is useful if you are going to modify the tables via migrations.
Schema Dumping and You will help you dump it to use as a reference for building migrations.
ActiveRecord makes some suppositions about the table naming and expects an id field to be the primary key with a sequential number as the type. Having the migrations would help you to refactor the tables and/or fieldnames and types, but you can do those same things via your DBM's command-line. You don't really have to follow ActiveRecord's style but doing so helps avoid odd errors and lets AR infer things to make your life easier.
Could try Magic Model Generator
Take a look at rare_map gem.
https://github.com/wnameless/rare_map
It works both on Rail 3 and 4.