using laravel migrations to access tables already created - laravel

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.

Related

How to create Laravel model from migration?

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

Create database schema and eloquent

I have a doubt about eloquent, it should be a very basic doubt but Im not understanding.
For example, if we build a db diagram in mysql workbench or something like that, then if we want to create the same shema in laravel we need to add in the migrations the necessary id´s right?
The eloquent relationships (hasMany, belongsTo, etc) are just methods to fetch the info that is necessary in some context but the database schema, the relationships are set with the ids in the migrations right? The relationships (hasMany, belongsTo, etc) are not mandatory to build the database schema?
The migration would define the foreign keys that form the relationships described by hasMany, belongsTo, etc.
A seeder would need to do as you suggest and get the id for the row in the associated table so it can be populated in the foreign key column.
You can, of course, do some seeding in your migrations, but that should be done under very clear and limited circumstance.

Laravel 5.3 Scaffold CRUD from existing database

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.

How to create unique relationship using NeoEloquent

I have just started using Laravel. I have not used Laravel Eloquent before. So I am directly using it on graph database using NeoEloquent.
I know I can create relationship using hasMany() and attach() methods. But I want to create relationship only once. Has anybody used NeoEloquent for creating Unique Relationship.
You may use the hasOne()relationship instead of hasMany() which will ensure that only one relationship of the specified type exists for the model.

rails 3:how to generate models for existing database tables

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.

Resources