Laravel 4 with Data Mapper? - php

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

Related

what point must be considered before using laravel eloquent relationships

i am working in laravel and i have used its method what i am keen to know that when we use eloquent relationships like
1.hasMany
2.belongsTo
3.hasOne
4.belongsToMany
so when i want to retrieve data from both tables how do we use laravel methods to get data as per our need
i have done these methods as follows
TABLE::select([
'field1','field2',
])->with('RELATION_NAME')
->get();
but i failed to get the data from another table so i want to know what are the tips and what should be kept in mind using the eloquent as i have not used it before so it will a great help

Repository pattern in Laravel using Eloquent

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.

Is there any method like code first in Laravel?

I am searching for a way for creating database by my models structure in Laravel 5.5.
I know many Orm Like Doctorin Or ActiveRecord has this feature and so some micro Orm such as redbean has this feature but I cannot find a good reference for this ability in eloquent ORM of Laravel and if there is any method has it migrations which alter my existing database according to my model changes?

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..

fetch database metadata from laravel

Is there an easy way to fetch db metadata from laravel?
I was looking to leverage Breezejs EntityManager, but I need to fetch the metadata on my DB and I was hoping I wouldn't have to define this twice.
Update
specifically, I'm looking to acquire schema metadata about entity structure
http://www.breezejs.com/documentation/metadata-by-hand
http://json-schema.org/examples.html
There's not direct method, but if you are curious and don't mind using DataMapper instead of ActiveRecord (switching from Eloquent to Doctrine) there's this repo that leverages support for BreezeJS generating the entity metadata from Doctrine ORM annotated models. They implemented an Employee Directory sample app, that can have laravel as one of the server variations.
I'm writing the metadata by hand for now (it's really not complicated), You can also write some code that can infer the models schema by parsing some query like 'DESCRIBE table_name' so you can stick with Eloquent.
EDIT: The answer to this question shows this DESCRIBE approach Can Eloquent models retrieve metadata on their rows?

Categories