Multiple Relationships in Laravel Query Builder - laravel

I have an Event model and a method called getEvents() which gets a list of events from my events table in my database.
Each event will in turn have delegates associated with it, and this is were it becomes complicated.
There are 3 tables, delegate_event (this is a pivot table, that links the delegates to an event based on event_id and delegate_id), delegates (a simple table containing an id field and a contact_id field) and a contacts table which contains information on a list of contacts.
The delegates table uses the contact_id to link to the contacts table and pull through various pieces of information associated with that contact.
I am using Query Builder and the leftJoin method to get most of the information I need for each event but I now need to pull through a list of delegates. http://paste.laravel.com/qSa
I can't work out how to join the contacts associated with delegates that are associated to the event via the delegate_event table.
Can someone help me out here please. Thanks.

You need a many-to-many table for event-user because multiple events will have multiple users (delegates) associated with it.
Here's what I have:
mtm_event_user: ID, event, user
events: id, name (of event)
users: id, contact
contacts: id, name, address, etc
Here's the query I have, you can modify the SELECT to suit which fields you need.
SELECT *
FROM contacts
INNER JOIN users ON users.contact = contacts.ID
INNER JOIN mtm_event_user AS meu ON meu.user = users.id
WHERE meu.`event` = 1
For multiple events, replace WHERE meu.event = 1 with ORDER BY meu.event

Related

Laravel Eloquent (eager loadable) custom polymorphic relationship

Using Eloquent, I'm trying to make a polymorphic relationship between some models that isn't working out. Simplified, it works like this: there is a model 'groups', which obviously can contain some groups. Now each of these groups can either contain 'members' or 'guests', so I have a table and model for both of them. These are not similar to each other. My groups table contains a column in which model to use (members or guests). The structure is as follows:
groups (\App\Models\Group)
id
name
model (either \App\Models\Member or \App\Models\Guest)
members (\App\Models\Member)
id
group_id
first_name
last_name
address
email
.....
guests (\App\Models\Guest)
id
group_id
name
So the relationship 'people' in a group retrieves either the members or the guests. Using the default $this->morphTo() function using the group_id is able to retrieve the correct results, but due to its logic only returns one result, while I need all results. The rest of the app is indifferent to whether the people are members or guests, so I do not want to split those two up here.
Any ideas on how to accomplish this? I'm trying to eager load the relationship.

Getting Data together for program and campaigns from three tables

I'm facing the following problem.
I have a list of Back in Stock requests. Each request gets its own RIID and the email is hashed. I also have the product id of the product.
What I then created is a SQL view that pulls the contact data from the profile list (contact list) and the product data I need from a SUP feed. This SUP feed is not connected to the contact list via RIID or similar.
My problem then is how to get the data from all 3 lists into a mail for personalization. I can't access the data from the SQL View, can I? The Back in Stock list is not provided with email addresses and the product list is disproportionate to the contacts. Does anyone have an idea?
Here are examples:
BIS.List fields:
Email_Hash, RIID_, PRODUCTID
CONTACT.List fields:
Email, Email_Hash
Product_Feed fields:
PRODUCTID, PRODUCT_IMAGE
SQL View RESULT fields:
Email, RIID, PRODUCTID
In the Campaign Dashboard, open the Data Sources window. Add all the supplemental tables you need (remember to assign lookup fields and aliases to each table).
In your Campaign source, use the #data command (RPL) to fetch the fields and filter the results from each of the suplemental tables.
RPL Documentation: RPL_Reference_Guide.pdf

Design DB for chats between users

I have one problem with designing database structure for my app. I have 3 models: Events, groups and users.
Relations:
Events - Users - many to many.
Groups - Users - many to many.
There will be available one chat withing group and one inside event. Additonally in the near future I am going to implement chats between two or more users.
I'm a bit confused what is the best way to design it. I created Chat model and many to many relation between chat and users.
First idea: The members of group and event chats will be stored in respectively event_users and group_users tables. Only chats between users will be stored in chat_users.
Second idea: chat_users will be synchronized with event_users and group_users tables. The advantage of this way is that I will have simple separated logic to manage chats and there will be not many complicated queries to DB.
Thanks for any feedback or maybe other ideas.
I believe that Chat has a polymorphic association with Group and Event (and maybe User as well). Some solutions to the problem are provided here:
https://www.slideshare.net/billkarwin/practical-object-oriented-models-in-sql/29-Polymorphic_Assocations_Solutions_Exclusive_arcs
Could something like this work?
ChatUser
id
user_id
chat_id
Chat (exclusive arcs method)
id
event_id (nullable)
group_id (nullable)
description/etc
ChatMessage
id
chat_id
user_id_from
user_id_to (nullable)
date
message
Assumptions made:
event/group can contain multiple chats
user can belong to a group but not to group chat
Besides, Laravel comes with a native polymorphic relation handling solution. It could fit here as well.
User can participate to an event and event can have many users who participate to that event, in this specific case we need to use as Many to Many relation Laravel One to Many Relation
event_user
id
event_id
user_id
User can be member of many group and a group can avec many member, in this case we need a Many to Many relation Laravel Many to Many Relation
group_user
id
group_id
user_id
chat can be related to an event or a group, so we save the type of the chat witch can be either Event or Group and the corresponding ID of that event or group. so if we have a particular chat we can retreive all users related to that chat through the corresponding event or group it depending of the chatable_type in each case, to learn more see Laravel Many To Many Polymorphic Relations
chats
id
name
chatable_id
chatable_type
we need also one table to save all conversations for specific chat
messages
id
chat_id
message
created_at
and for users chat you can create a separate table witch can save the related information about that chat specifiquely
conversations
id
sender_id
receiver_id
message
all sender_id and receiver_id will be foreign key witch reference some id on users table

How to insert into multiple tables with foreign keys in Joomla?

I want to know how to handle mysql tables created with constraints in joomla.
for a example,
theater_table
id , name, description, image, address, tel, fax ,email
theater_facility_table
id, theater_id, facility_id
facility_table
id, name, description, image
Facility table already filled with data and id is the primary key. When creating a theater I am adding facilities to it. I created facility and theater JTables.
Do I have to create theater_facility JTable too?
Using theater Model class how I insert data to theater_facility table. I know I can insert data after theater stored successfully creating and calling storeTheaterFacility() method where it contains insert query to save required information. But I feel it can't be a good method to do so. Please help me to solve this.
Depending on how you implemented the theater - facility relationship, you can handle insering new data in different parts of your code. I mean, if for example your JTable class (the one that loads theaters) is loading/saving the theater-facilities relationship too, then the same class should delete it.
May be you can take a look at other components (for example, com_content, which relates an article to a category, or K2, where you can have multiple tags related to multiple "items"(articles)), so you can take a look on how do these components handle these kind of relationships.
Another important point you shouldn't forget is to update your facility model / table to delete records from the relationship table upon facility deleting.
I hope it helped!

one to many relationship in Entity Framework on the same table

I have two tables, in which one user can have several contacts:
User(UserId, UserName)
Contact(UserId, ContactId)
Suppose I would like to get all the ContactNames from the UserNames in the User Table by the Userid.
Note that the Contact table cannot be seen in the current data context, it has been turned into a many to many relationship
How can I do the query?
What If I need to insert or delete?
I tried the "include" method but it does not work. Do you have any suggestions?
Thank you very much.
var id = 1; // id to find
context.Users
.Where(x=>x.UserId = id)
.SelectMany(x=>x.Users)
.Select(x=>x.UserName)
.ToArray();
After generation from db your model has 2 sub-collections: Users and Users1.
One of it corresponds to users, that are contacts for current user.
Another stores users, that current user is contact for.
You can rename them to represent their meaning to Contacts and ContactsFor via editor.
If you still want to have 2 types instead of Type+(Many-To-Many Ref), then in editor you can delete reference, create new entity Contact, setup all mapping, add references. After all this is done - you will have model look like this:
To Achieve this:
Delete Many-To-Many reference
Crete new Entity Contact
Add properties ContactId and UserId, set StoreGeneratedPattern to none
Add mappings for Contact
Add associations for Contacts and ContactFor
But it's not that easy.

Resources