Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel without Eloquent & database migrations?

I'm a PHP programmer for 12 years now, and pretty much re-invented the wheel many times, building my own framework for our closed-source web-app, which is offered as a hosted solution, using the same shared database for all customers.

Now I'm trying out Laravel 5 and noticed that almost every example uses Eloquent and database migrations. To me it seems these kind of things are targeted towards simple databases and people who don't like SQL or database-design (but I might be wrong).

Our MySQL database contains 100+ tables, a lot of stored procedures and many triggers which I just can't imagine doing in an ORM. We use Navicat for database-design and testing SQL-queries. For upgrading the database to a newer version of the app, we already wrote some nice scripts and even visual tools.

So basically my question is if Laravel is really intended to be used with Eloquent and migrations or that I'm really missing out a lot of functionality without them.

What do you recommend?

like image 620
Dylan Avatar asked Dec 23 '15 03:12

Dylan


3 Answers

Using an ORM simplifies your life as most common scenario's are already covered. Fetching data, managing relationships and eager/lazy loading are a breeze. It also protects you from any injection vulnerabilities you might create when writing your own hardcoded queries. Of course not all scenario's can be handled by an ORM, hence the possibility to write RAW queries. Using Laravel's Query Builder you could do something like this:

$results = DB::select( DB::raw("SELECT * FROM users WHERE username = :username"), ['username' => 'johndoe']);

If you want to execute things like ALTER or SET's then you can use DB::Statement.

So just to note, Eloquent is Laravel's ORM while the Query Builder is the layer for building your queries in a safe way.

So whether or not to use Eloquent is up to you, I believe they've done a good there with a solid ORM but you're always free to implement another ORM like Doctrine, Data Mapper, etc. There are Laravel bindings for most of those.

Edit: Worth mentioning is also that an Eloquent Model provides some handy extra's that also simplifies things, like a JSON convert __toString, protected attributes, date casting, etc. When fetching multiple models they will be stored in a Collection, an Arrayable that provides even more methods to make for happy times. Check it out: http://laravel.com/docs/5.1/eloquent-collections

like image 195
tomvo Avatar answered Oct 18 '22 00:10

tomvo


You can run simple SQL queries in Laravel if you want, but indeed most examples are using Eloquent.

I've been doing project with over 100 tables and it most cases it's possible to use Eloquent instead of putting raw SQL queries each time but it's rather my preference.

However you mentioned that you have many stored procedures and triggers in database. To be honest you should rethink that because now you might be putting to much business logic into Database and your logic is both in Database and in Application.

I saw a couple months ago such database in MsSQL and it was horrible - nobody really knew what is happening and when you wanted to migrate this database to MySQL and Laravel application it was a big problem because there was too much logic in database (I haven't took part in this project only took a look at it)

like image 20
Marcin Nabiałek Avatar answered Oct 18 '22 01:10

Marcin Nabiałek


Its upto you,

Laravel migration is aimed for keeping database version (while using version controller) also the eloquent is aimed for simple relation mapping between tables for complex situations like multiple Join and all its not recommended due to performance issue then you can choose Query Builder it gives much better performance , You need to write plain query in Laravel use \DB::statement();

The Laravel is a perfect match for Angular Js, more Laravel is just a wrapper of few nice PHP components that is capable for providing rapid results.

hope it helps..

like image 3
Jobin Avatar answered Oct 18 '22 01:10

Jobin