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');
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 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.
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 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
See the image please
I am saving admission classes fields data in array/json encode format,but i want to search all data where id is 7 from admission classes. how can i do it in larave.please guide and see the image above
So since JSON is text, you can search like so:
Model::where('field', 'like', '%7%')->get();
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();