Map existing Database table for Laravel

23,545

Solution 1

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.

Solution 2

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!

Solution 3

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.

Solution 4

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

Share:
23,545
Sangoku
Author by

Sangoku

Senior PhP developer @ DIG.at Legacy code is a baitch but one which can be tamed with enough time.

Updated on July 09, 2022

Comments

  • Sangoku
    Sangoku almost 2 years

    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
    Sangoku over 10 years
    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.
  • duellsy
    duellsy over 10 years
    you can use Jeffrey Ways laravel generators to streamline things a bit more, I'll add this to the answer
  • codecowboy
    codecowboy about 10 years
    @duellsy which command generates a model from an existing table?
  • duellsy
    duellsy about 10 years
    @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.
  • Tom
    Tom about 10 years
    Actually, you don't need to do this manually (at least not anymore). 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.