10

I am looking for a way to map existing tables in a project with the Eloquent ORM and use them in code. I use a MySQL database and plan to migrate to MSSQL. Any way points are appreciated.

Sangoku
  • 1,588
  • 2
  • 21
  • 50
  • 1
    What exactly do you mean by mapping? Using eloquent with the existing tables, or changing the database structure to work with Laravel. But, I guess, for both cases, you need to provide the structure of your database and tables. – Umut Sirin Jan 02 '14 at 22:23
  • I mean i have an existing db, u want to make for each table i already have a seed and to create the map 1-> many model definitions. – Sangoku Jan 02 '14 at 22:28
  • @Sangoku have you succeed in creating all relationships after the "mapping"? – Sefran2 Jul 06 '16 at 20:32
  • Yes, I succeded, but it took a while and i had to build a definition reader which requires a pretty high mysql user rights... is not idea but did the job. – Sangoku Jul 07 '16 at 07:50

5 Answers5

11

You'll have to do this manually.

i.e., create an eloquent model for each of the tables you want access to in your code using eloquent.

If you don't have timestamps named created_at and updated_at, in your model you can disable those columns.

Manually

If you have a users table you could 'map' it with a user.php file in your models folder like this

class User extends Eloquent {

    protected $table = 'users';

    public $timestamps = false;

}

Via artisan

You can use Jeffrey Ways Laravel Generators to help streamline the initial creation of your models, however you'll still need to make the timestamp modification manually.

duellsy
  • 8,497
  • 2
  • 36
  • 60
  • isn't there a database crawler? i can make one out of my head, just asking if there is a composer command or something already done before... hate the reinventing the weal. Like yougive it a table name and he creates the database model for you. I am about to make such crap, and would hate to remake it. – Sangoku Jan 02 '14 at 22:31
  • you can use Jeffrey Ways laravel generators to streamline things a bit more, I'll add this to the answer – duellsy Jan 02 '14 at 22:38
  • @duellsy which command generates a model from an existing table? – codecowboy Feb 17 '14 at 20:45
  • @codecowboy I don't believe there is a command for this, that's why I was suggesting you need to do it manually, but the above answer should help you with this. – duellsy Feb 17 '14 at 23:51
  • 5
    Actually, you don't need to do this manually (at least not anymore). https://github.com/XCMer/larry-four-generator should help you out...And you can use that or that plus the Jeffrey Ways Laravel Generators to help with scaffolding, etc. – Tom Mar 14 '14 at 20:27
6

This looks like an old post, but it was edited a couple of days ago, so I don't know if the original author is looking for a solution again, but if someone needs this info, here is a packagist package for Laravel 5 to do what you are asking.

Laravel 5 model generator from existing schema: https://packagist.org/packages/ignasbernotas/laravel-model-generator

Hope that helps someone!

Dash
  • 317
  • 4
  • 4
2

There is also a Eloquent Model Generator library. It can be used for generating Eloquent models using database tables as a source. Generated model will include relation methods, docblocks for magic field and relations and several additional properties.

Andrii Mishchenko
  • 2,626
  • 21
  • 19
1

Another here: https://github.com/Xethron/migrations-generator.

You'll only want to use these generators for local development, so you don't want to update the production providers array in config/app.php. Instead, add the provider in app/Providers/AppServiceProvider.php.

For more details look here - https://packagist.org/packages/ignasbernotas/laravel-model-generator#user-content-installation

Meetai.com
  • 6,622
  • 3
  • 31
  • 38
0

You can also use SQL Server Migration Assistant (SSMA) to port the database to SQL Server, but you will still need to write your own models to match the schema.

http://blogs.msdn.com/b/ssma/ http://www.microsoft.com/en-us/download/details.aspx?id=43688

Still this might help get halfway there, from both sides of the puzzle.