make People, Location and History descend from an entity, like Users, then with your File table, use the id_user to make the relationship
User
id
1
2
3
People
id user_id
1 1
Location
id user_id
1 2
2 3
Files
id user_id
1 1
2 1
3 3
This approach seems like the relationship will break if the same user_id is in both the People and Locations? Based on your example above, how will the relationship know that the I should be pulling the photo for a location versus a person? What am I missing?
If you have a many-to-many relationship, then a pivot table is appropriate. There are different approaches to such a problem, but the other options are not very clean, can be prone to error, and if you want to add another entity that also can upload files, then you will be fighting against your code to try and implement it. Creating a pivot table for each is proper database structure and you will be able to associate one file with a person and/or location and/or history, etc.
Thanks. That is the direction I'm going to keep going in. It seems to make the most sense.
Hi gues i have same issue with what you are going true here (http://laravel.io/forum/06-26-2015-table-relationship-for-images-on-laravel-42-issues) What i am building is a classified ads website, but guest users can upload pictures before they post their ad, and the image is an ajax file upload, what i tried that did not work was that, first we will upload, then save the data called "tempimageuploads" but the unique id will be the app_token which is laravel session token, then the instance the user clicks a post an ad button, it goes to this table "tempimageuploads" based on the unique id and moves it to a permanent table to store the images data "permimageuploads" which by this time the user turns from being a guest to a member so by that time the users email is known and an user_id is generated for the user. But where the issue is, is that when we try to access the "petsads" table which holds the users posted classified ad both pending and active ones, but i want is a relationship between the "permimageuploads" and the "petsads" to check how many pet images where uploaded to show in the user petads listing both as a thumbnail or as a viewmore page to see more of the pet images. So do you think i should use the app_token field and transfer it to the "permimageuploads" table and to the use it as a file_id for relationship between the "permimageuploads" and other tables that will reference the file_id field. Second the laravel session token (The one that generate when you ) does it expires on logout or when the app is not used after a long time, and can it be used as a unique id.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community