Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I want make relationship with attribute but i dont understand how to do that with laravel ORM Eloquent, can somebody help me? Thanks in advance
If I'm not mistaken, you want to have a many-to-many relationship between your Mahasiswa and Team. Basically, it means that you have many teams, and each teams has its own members, so does members, each member can have teams. So you will need an intermediate/a pivot table between those two, for ex. mahasiswa_team. Then, inside mahasiswa_team table, you could add the role column to define each member's role.
For many-to-many relationship inside Laravel 8, you can see the documentation here.
If many-to-many relationship is not correct, then my last guess is you want the one-to-many relationship between Team and Mahasiswa. In that case, you want to move your role column to the Mahasiswa table. For one-to-many relationship, you can see the documentation here.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I need all reports with all officers included in the participants column.
please check the attached image.
https://prnt.sc/CRoELD48J-L5
You should really have a participant pivot table with report_id and officer_id, and create proper relationships.
What you're asking for is possible through loops and lazy loading, but it will be extremely slow and unoptimized.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
now I teaching Spring, and create simple project.
In my program I have User and Ore(Iron, Coal) but i think in future i add new ore or same goods.
And I don't know how implement many column.
If I create 50 different resources, these are 50 fields in the User class, but this is very inconvenient, how can I create such a relationship in the database. So that many resources can be added quickly.
I thought about design patterns, but I don't know which one is better to choose.
The project is built on Spring, PostgreSQL, MVC
You are misunderstanding the concept of a database. You don't add a column for each entry. Instead, look at your items, ores in this case, what do they have in common?
Could be something like
Id, OreName
Or could be somwthing complex like:
Id, OreName, price + a bunch of other columns with relations
It all depends on your requirements. I highly recommend you check out a database design video. Those would help you tremendously.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have two table with relationship many to many:
products
product_categories
So how do I name the pivot table correctly?.
If there is no particular reason to create different category tables for product and news, I think you should create many-to-many polymorphic relations.
If I went with many-to-many rather polymorphic, I would have created my pivot tables like so
categories_products
categories_news
In your relation functions, you can determine the pivot table name. The example in Laravel documentation:
return $this->belongsToMany('App\Models\Role', 'role_user');
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Each user can follow many categories (many to many),
Each category has many posts (one to many),
I want to get all posts followed by the user and sort them by date.
Is there any way to do that with Eloquent?
There may be a different way, but how I'd probably do it is with two queries:
// Get the IDs of the categories
$categoryIds = $user->categories()->pluck('id');
// Pull the posts with those category IDs
$posts = Post::whereIn('category_id', $categoryIds)->get();
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
When I am working in SQL Management Studio I am accustomed to mapping relationships quite often (usually one-to-many). Generally I do this to create various custom Views to help me optimize and otherwise formulate table outputs etc. So i understand and see the value of defining relationships within tables of an SQL database.
Now I am getting into Entity Framework (moving from Linq2SQL). Primarily using SQL database driven model designer. And I see how to map relationships in the EF designer. Not too hard. But my question is...SO WHAT?
Let's say I map the two tables 'Invoice' to 'InvoiceItems' (one to many) in the EF designer. How does this help me? Why not map the relationship on the SQL server and import the view to do whatever it is that I want to do? Does it help with formulating LINQ queries? If i delete an invoice record from 'Invoices' does EF automatically delete the child records in 'InvoiceItems'?
I am pretty jazzed to by utilizing EF, but I am not understanding the significance of relationship mapping within the EF designer. Thanks for any clarification here.
How does this help me?
It helps by adding navigation properties to your entities so you do not have to constantly join two entities - so your code looks like:
foreach(InvoiceItem item in invoice.ItemDetails)
instead of
foreach(InvoiceItem item in db.InvoiceItems.Where(ii => ii.InvoiceID == invoice.InvoiceID))
Why not map the relationship on the SQL server and import the view to do whatever it is that I want to do?
You certainly can if you do database-first design. EF will automatically create navigation properties that reflect the relationships in your database.
If i delete an invoice record from 'Invoices' does EF automatically delete the child records in 'InvoiceItems'?
No, but if your database cascades deletes then they will be deleted when the parent is deleted. Otherwise you'll need to intentionally delete child items. If you are doing code-first development that you can use the Fluent API to add cascading deletes to the EF model.