Repository pattern in Laravel using Eloquent - php

For a project at work I'm creating an API in Laravel. I wanted to use MongoDB as database driver which is new to me. So I want to use Eloquent with MySQL in the beginning and when I'm confident enough switch to MongoDB.
I was reading some tutorials about the repository pattern in Laravel and saw that some returned an eloquent model, like this one. It seems to me that when returning a model on for example create($data), you're limited to database drivers which are suitable with Eloquent. I was wondering if it is a good practice to return an eloquent model. Because if I want to use MySQL for now and in the future MongoDB (without the Laravel MongoDB package), I need to rewrite some code in the controllers because the use the eloquent models instead of an array (for example). Maybe a 'wrapper' between the model and your code is a possibility? Any good recommendations, tips or thoughts on this?
If you're wondering why I want to use MySQL for now and later switch to MongoDB, the reason is there is a time limit on the project. I'm still learning Mongo. So to fulfil the needs of my employer, I'm not using MongoDB until finishing the first version / prototype.

If you use eloquent and its methods, you should be good. Just do not use the DB::raw() method as it may break your query if it does not match the DB engine you are currently in.
I wrote a repository pattern article if you want to look at it (with tdd).
But if you really want future proof, you can mix Doctrine w Laravel since Doctrine is a Data Mapper Pattern. Link
With laravel-doctrine package, you persist the data in your db engine that you prefer.

Is something like laravel-mongodb good?
Extending Eloquent to use MongoDB instead of MySQL.

Related

PHP extract data from mysqlDatabase

I'm getting started with PHP and I wanted to know if there is a way that you can extract data from the DB in a model.
If I have a Users table in my DB, I'm looking for something like Users.all in rails that will use the model to extract all the entires in the Database.
Does PHP offer that type of functionality?
The closest to Rails is Laravel. Laravel's database layer Eloquent was already mentioned in the comment. Eloquent: Getting Started
If you go for an ORM instead of Eloquent's active record pattern Doctrine is the most popular project. Doctrine Website
Pure php doesn't provide an ORM out of the box ( I suppose you can install a package or something), but if you are using Laravel (since the tag in your question), you need to create a model (supposing you have a migration for users_table)
php artisan make:model user
then simply 'use' the model in any controller
use App\User
and get all users :
$users = User::all();

Are laravel relationships enough to construct a good database?

I am starting a project which requires some kind of hard (complex) database structure (meaning that It's not easy to draw a database structure).
My questions:
I know very well how laravel relationships work. Will those relationships be enough to construct a database by following it and then the database must be constructed so good that code written in application should be super flexible.
If those relationships are not enough, what do you advise me to do?
Eloquent Relationships are good for almost all cases, otherwise if you need a more complex query you can use Fluent Queries
Query Builder

zend framework muti-table query prototype

The tutorial of zend framework show that I can use Zend\Db\ResultSet\HydratingResultSet to return a model object using the dbAdapter ,select query and a model prototype. But in most case writing the website code I use multi-table query and the hydrator can not fix this problem but just a single table query. How should I deal with this kind of problem.
hydrator doc
And this problem can also happen when encounting the pagination which also only take one prototype.
paginator doc
Depending on the query, you might need a custom ResultSet that knows how to deal with the multi-row potential of a multi table query, and mapping that dataset appropriately.
Additionally, if you find yourself getting into very advanced object relationships with Zend\Db, that might be the time to start considering Doctrine2 (or a similar ORM).

Best way to get/set columns dynamically from a SQLite3 database?

...or I really need to create a class for each Table? then.. everytime i changes table structure i need to update the code..
You could use an ORM (Object-relational mapper) such as Eloquent, which is included in Laravel, and then just create an model (class) for each table in your database. Eloquent automatically maps each field into a PHP object. If you haven't ever used an ORM, I highly recommend you check out Laravel... it's what made me stick with PHP and I do almost all my projects using Laravel. Best of luck!
Adding to BakerStreet Response.
Eloquent fits your needs as the ORM itself will fetch all the columns you specify if you leave it as default. By default the drivers that it works with are: mysql, postgreSQL, and Sqlite.
Eloquent itself can be downloaded without Laravel being involved.
Please refer to Jeffrey Way's Laracast for instruction:
https://laracasts.com/lessons/how-to-use-eloquent-outside-of-laravel
...thanks all, ended up writting my own magic class:
DBIntrd - Simple PHP framework for SQLite3 databases
Tired of spending time writting a bunch of code to create PHP classes & methods for SQLite tables?
DBIntrd is magic way to dinamically instance objects and persists data at SQLite3 tables..

Laravel 4 with Data Mapper?

Does anyone know if Laravel has some sort of library/plugin or tweak to use Data Mapper or to make Active Record behave like Data Mapper?.
I'm not a Laravel user,so perhaps Eloquent is using a mix of Active Record and Data Mapper and I'm not aware of that.
So, we can implement the Data Mapper pattern into Laravel using Doctrine;
Packagist: https://packagist.org/packages/atrauzzi/laravel-doctrine
Laravel Doc: http://bundles.laravel.com/bundle/doctrine

Categories