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
Related
I'd like to know what is the best way of creating models using the Laravel framework? As far as I know, the model is used for the connection to the DB table and then, you can do whatever you want with its data.
Creating a new model per DB table (as stated here) isn't good and models should be created per business object. Not sure what it means. I'd like to access the same table and get some information from it using the Eloquent, so similar to this:
<?php
// Get the model class
use App\Post;
// Grab some data from the posts table
$posts = Post::all();
But it requires having a model. Using the select method, e.g.:
$posts = DB::table('posts')->select('title, body')->get();
Doesn't require having a model so I don't quite understand what's the point of models? I couldn't find enough information inside the Laravel docs: https://laravel.com/docs/5.6/eloquent.
As a full explanation of your question:
Using method:
<?php
// Get the model class
use App\Post;
// Grab some data from the posts table
$posts = Post::all();
will be the same as running a query like:
<?php
$posts = DB::table('posts')->select('*')->get();
but the biggest difference is that the first returns an Eloquent object, while the later returns a normal object. The first model requires a model, which extends eloquent, the later method is using the DB class, which does not extend eloquent. So when you want to use Eloquent you would have to use models.
Creating a new model per DB table (as stated here) isn't good and models should be created per business object. Not sure what it means.
Entities are things like Users, Posts, Comments, messages, but not relation tables such as user_has_posts or user_has_messages. Not entirely sure how else to explain it, but i'm sure you will get the idea.
Also keep in mind, models are supposed to only contain database logics, I've seen it plenty of times where people use models to filter arrays but this should not belong to models.
I'm looking for a proper way to handle and store relations between data in my DB and data from a third party API. I use Laravel 5.
For example, I have a Project model (id, name). Also, I have an API which I can call from PHP and get back a JSON with Articles list.
I need to save a many-to-many relation with a junction table between Projects and Articles.
Because Articles data comes from API I do not have a local table with Articles so I can't use Eloquent to deal with relations.
My question is how to do it right.
I've thought of 2 possible solutions:
1.) Use some lib that can "map" an API as an Eloquent model. But I've only found abandoned projects. And overall this solution looks like overkill in such simple situation.
2.) Use query builder and manually handle this situation to save data about relations between Projects and Articles in the junction table.
But if I'll use this option it will be hard to deal with updates etc.
I'm doing a web app here using Laravel + AngularJS and I have a question.Do I need a model for each table that I have in my database? There are 87 tables in my database and I need to query all of them according to with the input that the User wants.
I just want to make sure with all tables must have a model file or if just one is enough.
There are 2 ways by which you can access your DB tables:
Eloquent ORM (dependent on Models)
DB Facade Query Builder(independent on Models)
Former, is more clean and best approach to perform DB query and related task, whereas latter is not clean, and it is going to be difficult for you to manage large application, as you told there are 80+ tables in your application.
Also, if you're using Eloquent way, then it's also a better to have a base model, which will have common code which you can inherit in child models. Like if you want to store "user id" who did some DB changes, then in the boot function, you can write Auth::id() and assign that value to changed_by field on your table.
In DB Facade way, you've to hard code table name every time you're performing DB operation, and which leads to inconsistency when you found that you've to change the name of the table, it's a rare scenario still it'll be difficult to manage if in a file there are multiple tables DB operation going on. There are options like, creating a global table name variable which can be accessed to perform DB operation.
Conclusion:
Yes, creating 80+ model for implementing Eloquent way is painful, but for a short term, as the application grows it will be easy for you to manage, it will be good for other developer if they start working on it, as it will give a overview of DB and it will improves code readability.
It depends on how you'd like to handle queries.
If you'd like to use Eloquent ORM, you need model classes to handle objects and relationships. That is a model for a table, except intermediate relationship tables, which may be accessed through pivot attribute.
Raw SQL queries are also supported. You don't really need model classes for them, as each result within the result array will be a PHP StdClass object. You need to write raw SQL though.
See Laravel documentation.
My question is more theoritical, as I am not quite sure if it is a better way to create a model in Laravel for each table on database, if yes, what would be the benefition of it?
I am using Laravel 4 Eloquent for the ORM.
Thanks
The basic answer is yes, you should have a model for each table.
But the long answer is "it depends". As for what "depends" is, it is something that comes with experience and also your design criteria. There is no 100% right answer that can be used everytime.
As a principle if you plan on accessing data from tables using Eloquent, then you generally need one eloquent model per table, so you can access the table using Eloquent functions.
As a principle you dont need a model if you never use Eloquent to access the data. i.e. perhaps you have a table that you only use the query builder on.
I'm slightly confused and need a bit of guidance if someone wouldn't mind.
Just a bit of background, my application is a website containing a simple survey (a list of questions with possible responses retrieved from the database) Thus far, I have created the models for the questions and responses tables and have defined the relationships within these classes - i.e., a question can have multiple possible responses whereas a response belongs to one question. I've used Eloquent to handle the retrieval of the data which works fine.
Now, I need to run a query using the query builder to retrieve the posted data because of some complex left joins that I cannot do using Eloquent, but I'm uncertain where to put these - do I put the query inside of a new model, or run it from my controller like I have when using Eloquent?
That code should be wrapped in a method of the model.. So if you need to do a crazy join for a user.. it would be $user->getSomeCrazyJoinedData()... you can also use a query scope if you want to pass arguments into it.