Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map existing Database table for Laravel

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.

like image 529
Sangoku Avatar asked Jan 02 '14 22:01

Sangoku


People also ask

Can we generate migrations from an existing database in Laravel?

Yes, We can generate migrations from an existing database. This package will Generate Laravel Migrations from an existing database, including indexes and foreign keys also. In Laravel 5.5 the service providers will be automatically get registered.

What is Laravel database?

Database: Query Builder - Laravel - The PHP Framework For Web Artisans Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.

How to retrieve a single row from a table in Laravel?

For more information on Laravel collections, check out the collection documentation. Retrieving A Single Row / Column From A Table If you just need to retrieve a single row from a database table, you may use the DBfacade's firstmethod. This method will return a single stdClassobject:

What can I do with it in Laravel?

It can be used to perform most database operations in your application and works perfectly with all of Laravel's supported database systems. The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks.


2 Answers

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.

like image 189
duellsy Avatar answered Oct 04 '22 06:10

duellsy


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!

like image 21
Dash Avatar answered Oct 04 '22 07:10

Dash