I am new to laravel and using laravel 8. I have following tables and I am not able to understand how to establish eloquent relation between them.
Table - Fabrics
id
user_id
name
1
1
fabric 1
2
1
fabric 2
Table - Mesh
id
name
1
mesh 1
2
mesh 2
Table - Designer
id
user_id
mesh_id
fabric_id
name
1
1
1
1
designer 1
2
1
1
2
designer 2
I have done all the basic relations. Each designer has one mesh and one fabric. there is a unique combination of fabric_id and mesh_id in designer table meaning one fabric and one mesh can not have more than one row. now I want the list of fabrics with the sublist of meshes with which designers are created. basically from the designers table instead of
with(designers) I want with(meshes). Please help. Thank you in advance.
Looks like a many to many relationship problem. See this documentation
There is a good example on many to many relationship.
You have a many to many relationship between fabrics and mesh table with designer table being the intermediate table linking both tables together.
Therefore you should already have meshes relationship in your fabrics table using belongsToMany(Mesh::class) and have fabrics relationship in your mesh table using belongToMany(Fabrics::class).
You can retrieve all the fabrics and its related meshes through its relationship.
$fabrics = Fabrics::get()
foreach($fabrics as $fabric) {
$fabric->meshes
}
Related
Information:
A sound company has many employees.
The employee has many positions in the company.
The company has many events. And for every event, they need a crew.
A crew is composed of many employees holding certain positions. An employee can hold multiple positions in a crew.
For example:
The list of employees is:
Employee 1 is a Driver, Sound Engineer and Stage Hand
Employee 2 is a Driver, Sound Engineer
Employee 3 is a Sound Engineer and Stage Hand
Employee 4 is a Stage Hand
Employee 5 is a Stage Hand
The event is called: Event 1
The crew is:
For the position of Sound engineer:
Employee 1
Employee 2
For the position of Stage Hand:
Employee 3
Employee 4
Employee 5
For the position of Driver:
Employee 2
Employee 1
Problem:
I believe this is done by using a pivot table that holds the event_id, employee_id and position_id
But when I follow this approach, I get stuck on feeding the data and the methods to use to create new data.
Is there a different approach?
you need two pivot tables. One for employee and position which hold employee id and position id. Another one between employee and event which holds event id and employee id. I think this is a better way to handle this. You can use the attach and detach method handling pivot table in laravel.
Sorry for asking this but I need to know how to do this:
Lets say that a mentor has 10 multiple mentees, is there a way to reduce the amount of fields I need to put in my database (mentee1-mentee10) ?
theres a mentor and student table in my DB
does it has something to do with one-to-many relationships?
sorry if this is too little information im giving, i'd provide more description on what im trying to ask here if you'd like
If a student can have multiple mentors, then you can use a M-N relationship and introduce a 3rd table student_mentor table that will map a student id to a mentor id.
--------------------------
| student_id | mentor_id |
--------------------------
Now, if you need to allow at most 10 mentors, then you'd have to build some triggers that will prevent inserting/updating of records that will violate this restriction.
To conclude, yes, you can reduce the field redundancy, however if you want to keep the 10 mentees constraint you will need to do some extra work.
If you have 2 tables, mentor and mentee, you could give your mentee a mentorId pointing to the mentor table.
And yes - this is very much a one-to-many relationship :)
If however, students can mentor many students AND students can BE mentored by many students, then you have a many-to-many relationship. These are usually modelled by whats called Junction Tables
We are currently considering migrating our database code in php to Doctrine2. It seems to us that most of the things we would like the do are possible with Doctrine2. There is only one thing which we are not sure if it is possible and how we should do that. Our database model can't be changed right now because another project is using the same database. We can only change one thing at a time. Of course we are also curious how to improve the database model but that is not our main concern.
The issues is like this. We have a product table containing basic product information like this:
[product_id | code | name | description | price]
4 AS84GD Steel brush Strong steel brush..... 25.50
28 HDD21N Sand paper Sand paper for wood..... 0.40
etc
We have different users for our product which al have different extra product properties. So we have an product_extra_field table which could look like this:
[extra_field_id | name]
3 weight
4 color
9 supplier
and finally the table with the extra data product_extra_field_data
[product_id | extra_field_id | value]
4 3 280
4 4 brown
4 9 company_x
28 3 3
28 4 yellow
28 9 company_y
How do we right the necessary Doctrine2 code to get this data in one object, so we could have an object like this?
[Product]
Id : 4
Code : AS84GD
Name : Steel brush
Description : Strong steel brush.....
Price : 25.50
Weight : 280
Color : brown
Supplier : company_x
I've added a tag for Hibernate because I can read (N)Hibernate code and translate the ideas to Doctrine2.
So far, I don't think you can achieve that with mapped superclasses.
But, you could use native SQL within the doctrine result set mapping:
http://docs.doctrine-project.org/en/latest/reference/native-sql.html
Those are one of the drawbacks when mapping between objects and relational data, I mean, if you really want just one entity instead of replicating the relationships from the database.
You could create several classes:
- Product
- ExtraField
- ExtraFieldValue
With the following relations:
One Product has many ExtraFields (One To Many relation), one ExtraField has many ExtraFieldValues (One To Many relation), consedering your DB structure.
Then you have to customize your getters/setters to get only the first result of the collections (if in fact your relations are One To One)...
Don't know if I'm clear enough.
I'm developing a site like "yellow pages" with more complex DB than I had ever experienced before.
For example:
This DB has 3 tables:
1. organisations – with some general information.
2. services - with list of all services that can be provided.
3. relation table.
Organisations table structure:
id name city phone etc.
1 CompanyName Farewell 987-65-43
Services table structure:
id ServiceType
1 purchase
-----
2 sale
-----
3 exchange
etc..
you get the point
And relation table structure:
id OrganisationID ServId
1 1 2
----
2 1 3
----
3 1 6
----
4 2 3
In website output I need a list of all organisations with all the services they provide.
I can't fully understand (even after reading Linking Models Together section in the CookBook) how I should combine all this tables using Cake's syntax and conventions.
I'll be glad if anyone can provide me a Cake's model/model's (how many I need?) example with ability to retrieve this data.
I can't comment on your question so I'll ask here...
Organisations can have many services and services can have many organisations? Is it HABTM relationship? (Just checking...)
If it is, I'll then edit this and try to give you the answer. :)
EDIT:
TABLES
1. table: organisations
2. table: services
3. table: organisations_services (sorted alphabetically)
1. table: id, name, city...
2. table: id, service_type
3. table: id, organisation_id, service_id (singular, sorted alphabetically)
MODELS
Organisation - inside your organisation model you must have:
var $hasAndBelongsToMany = array('Service');
Service - inside your service model you must have:
var $hasAndBelongsToMany = array('Organisation');
And that's all. Everything else leave to the beautiful Cake. :) If you don't understand something, feel free to ask and here you can get more information.
I'm trying to build a searchable database of acronyms and their definitions that are specific to a certain industry. It has been years since I've done any real programming, so I'm a little behind the learning curve.
I'm writing the code in PHP, and I'm using MySQL as the database. If this can be done easier in Postgres, I'm not opposed to switching DBs, but I can't use Oracle or any other commercial system.
So here's the question:
I'd like to set it up so that each acronym can: (1) apply to 1, multiple, or no specific organizations; (2) have 1 or more associated definitions.
The complexity--at least in my mind :D--comes in that it is conceivable that some organizations might have a single acronym with multiple definitions that all relate to that one organization. At the same time, the acronym may have 1 or more definitions that relate to OTHER organizations as well.
Am I over complicating this?
I'd like to better understand how to setup the table structure and relationships in MySQL--what fields and relationships would be in each table.
A SQL statement would be helpful if anyone feels so inclined, but I'm hoping to at least get a solid grasp on the database schema so I can get the tables created and some sample data imported.
Many, many thanks to all...
Dan
The solution should contain 4 tables: Acronyms, Definitions, Organizations, and AcronymOrganization.
Acronym(id, acronym, definition_id)
Definitions(id, definition)
Organizations(id, organization)
AcronymOrganization(id, acronym_id, organization_id)
If I understand your question, you can use three separate tables. First, have the table of acronyms/definitions, then have a table of Organizations. Finally, have an AcronymOrganization table, that just references a key from the acronym table, and a key from the organization table. This way, you can have as many acronyms for an organization as you please.
After you set up the database, you need to use a couple inner joins to join the three tables, collecting only the acronyms for the appropriate organization ID.
I'd just create an acronym table, an organization table, and a definition table. Put two foreign keys in the definition table: one for the entry in the acronym table, and the other for the entry in the organization table.
If you want to have a n:m relationship between tableA and tableB, then you need a third table.
table A. Fields : ID,name
table B. Fields : ID,name
table AB. Fields : A,B (A is a reference to A.ID, B is a reference to B.ID)
[TABLEA]1-----*[TABLE_AB]*-----1[TABLEB]
Example
Contents of table a:
ID Name
1 John
2 Mary
3 Piet
Contents of Table b:
ID Name
1 Microsoft
2 Google
3 Philips
Contents of Table ab:
ID Name
1 2
1 3
2 2
3 1
3 3
Then select everything like this:
select a.name,b.name
from a,b,ab
where a.id=ab.a and b.id=ab.b
Result:
a.name b.name
John Google
John Philips
Mary Google
Piet Microsoft
Piet Philips