In my Laravel 4.2 app I have several models of which use Eloquent ORM. I usually access the data through the standard functions (e.g. ::get()).
The issue I have is that with ORM I'm producing lots of queries when perhaps just a single custom query would suffice.
My question is, is there a way to execute a custom query but have those results populate into the my pre-existing Eloquent models.
Related
Which is the best method to use for the CodeIgniter 4 among the Model and the Query Builder class? What are the limitations of the CodeIgniter 4 Model and the Query Builder?
I searched a lot about this question. But I did not get any clear answer. For complex queries currently, I am using the Query Builder class. But I want to know what is the best method among the CodeIgniter 4 Model and the Query Builder class. Your answers are highly appreciated.
It depends on your needs and preferences.
Model class - This is used by extending the base Model class, and you can add your own functions/methods. It is not as flexible as the Query Builder, and it only has pre-defined methods.
Query Builder - Easy to build complex queries. But more room for errors if didn't handle it. Ex SQL injection if you failed to validate it properly.
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
There is a way (also presented here) to have create Eloquent ORM Models (as a part of a Laravel app) based on a database.
I am looking for the inverse action. I would like to write some Eloquent Model classes
and have them being craeted as tables in the database.
How can this be done?
This Eloquent documentation does not feature any information. Can it be that while there is a often the case 1-to-1-mapping between the database and the eleoquent models a table canno tbe created automatically given a Eloquent Model was provided?
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?
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?