eloquent relationship and database or three way relationship - php

i am struggling with setting up my database and eloquent relationships in a certain scenario.
This certain part of my application will be handling online orders.
basically i want an order to consist of multiple configured items.
i want configured items to consist of a base item (ex. a cheesburger) and also of toppings.
i have gone through several scenarios, but I am trying to make this as simple as possible. here is the quick and dirty story of what I have now.
I want a configured item to consist of three things. 1. the order id of the order it is associated with. 2. the menu item that it relates to (ex. cheeseburger, hotdog ) 3. and the toppings.
I am considering two tables that are full of relatively static information about the menu items and the toppings to be referenced from the configured item table.
I had originally considered creating a new menu item on every configured item, but I like the idea of just being able to look up items/toppings and applying them to a configured item. Im sorry if this is unclear. I am three days into this and my brain is absolutely in pain by now.
here are the relationships i am considering.
configured_item: belongsTo Order; hasOne menu_Item;
Menu_item: belongsToMany configured_item; hasMany toppings;
Toppings: belongsToMany configured_item;
I guess in a way my configured item table is a pivot table of sorts, but then it will need to be referenced by an order as well.
i know questions have been asked about three way relationships, but I cant find any info on tables that are relatively static like i am trying to use.

I finally caved and used two pivot tables. it all works, but i cannot help but feel there is a better way to handle this. It seems a lot of people have similar issues and there is no clear cut solution.

Related

How to add new columns if needed

I am new to MySQL databases and I'm trying to create a web stock and production database, using PHP.
In this inventory software, I am trying to create a table in which products are created and also their components inserted. But if a product has a different number of components, I wanted to know if there is any way to add more components columns from the management page of the website.
Another thing is that these components are taken from another table, and as long as a new product order is created, the quantity of those used components should be subtracted from the components table. (but this is a major issue, solving the first issue should be enough for now).
Yes, you can add, or remove, columns from a database table at any time.
However, I would not do this. You have to try and design a database that can handle products with a varying number of component. Normally this would be done this way:
Create a table for your products.
Create a table for your components.
Create a linking table product_components, to indicate what components a product consists off.
See: Using linking tables for many to many relationships in MySQL
You need to know about relationships in mysql. There are basically 3 types(some may say 4 though) of relationships available in a relational database like mysql.
Here according to your description you can use
1 to many
many to many
relationship in your database. This may help you-
https://afteracademy.com/blog/what-are-the-different-types-of-relationships-in-dbms

Three way Eloquent relationship

The project that I am working on requires a sort of sharing functionality meaning that when a person creates an exercise they can choose to share that exercise with another person and append a certain permission to that exercise (i.e read, write, or execute).
I have three tables(all of which have models): users, exercises, and permissions. In the middle I have an exercise_permission_user table that only has three columns: exercise_id, permission_id, and user_id all of which are foreign keys that point back to their respective tables.
The problem comes with establishing a three way many to many relationship among these tables in Laravel 5. More specifically, when a person shares an exercise, I need to input the id's of the exercise being shared, the user it is being shared with, and the permission that is being appended into the exercise_permission_user table. I then need to be able to query the user_id of this table and see all exercises that are being shared with a certain user. If the user Mike has an ID of 3, then I would like to query the middle table for that ID and find the exercise he has access to as well as the permission that he is being granted.
I am still in the learning process when it comes to eloquent so any help would be greatly appreciated. I am not necessarily looking for someone to build this for me, just some help that will give me the information necessary to do it on my own. Thanks to all that help!
I've struggled with this issue a couple of times. As far as I could research, I didn't find a Laravel native way of coding this kind of three way many to many relationship. What I generally do is to create a model for the pivot table. So, a SharedExercise model (or the name you want to use) with a protected $table property set as 'exercises_permission_user'. Inside that model you set the relationships with user, exercises and permissions. Then, you can write:
$sharedExercises = SharedExercise::where('user_id', $userId)->get();
Pay attention to the table and model naming. I usually name tables using laravel's conventions, but when I have this 3 way many to many, I try to find a more describing name than the convention. So, for example instead of exercises_permission_user and ExercisePermissionUser model, maybe shared_exercises and SharedExercise names are better.
Note that this isn't THE way to do it. It's how I do it as a result of not finding a convention in the documentation.

Laravel 5 Splitting up a Model

PHP noob here.
I have an Item model which is responsible for adding items to my cart.. However the items I'm adding are not simple products. Each item type has a complicated formula for generating a customized quote based on the users selections and personal discount, etc.
The simplest way I can think to do this is to have functions for each product within the item model with each unique formula in. However, this will get messy very fast.
What is the best practice for splitting these into separate files and then including those files in the model, just for keeping my code clean and seperate?
So for example I have aircraft, cars, boats as categories, each product within each category has it's own formula but I at least want to keep aircraft formulas separate from boat for ease of reading.
Is it best to create an aircraft model, boats model etc and have them all use the items DB table and then once they are added I can control them all from the Item model?
I may have just answered my own question, but I don't know :D Learning loads but still a noob.
I would suggest two things:
Extract logic from models
I would extract as much of the business logic as possible from the models - to, say, several formula classes (one for each different formula), which you can easily execute wherever you need to.
If the data you will save differs between each type, you may want to look into...
Polymorphic relationships
See the laravel docs on this for specifics.
You would have a Product model, that has a polymorphic morphTo relationship (called "type", maybe) to several other "product type" models (that all have an inverse morphOne back to the Product).
Polymorphic relationships were the first thing i thought of while reading your question, but on re-reading it, the data structure for each type might not be different. If that's the case, ignore the polymorphic bit!

Database Design/ERD - Quiz Engine

I've been using this site as a great resource since I started at school - but I've never had a problem this specific before and I was hoping for a bit of help. I was never the best when it came to figuring out database structures, and I've been tasked with creating a PHP/MySQL test engine with some rather specific specifications.
So you can better understand what I'm going for here - I am trying to take into account the following:
Administrator and student login are required, and provide different levels of access.
An administrator should be able to build one or more tests and assign it to one or more students.
For each question an administrator builds within a test, the administrator should be able to assign a point value.
A test should be able to present one or more questions.
Your application should support three basic questions types: true/false, multiple choice and fill in the blank.
Final results will display an overall score, as well as a student’s response to each question.
A student should be able to see final results for only test they have access to.
An administrator should see results for tests from multiple students.
Students are not allowed not retake the same test.
Mostly, I am trying to deal with the basic structure. I had five tables at the start, I condensed the Question/Answer section into one table - and excuse my poor attempt at switching around the relationships here, because I've had them 20 different ways it feels:
Am I on the right track? Any suggestions?
A good rule of thumb with database design in N-1. For all tables that have relationships, you should have N-1 relationships (where N is the number of tables). Tables with circular references are a no-no. Putting as the security components and just looking at test/questsion/answers, you want a design that has Some basic objects:
Admin (or teacher) table
Test table
Student Table
Question Table
You didn't mention it in your question, but if you want each test to be associated with a class that the teacher teaches, you will need that as well. With those four, or five, tables you should be able to create your relationships. Hint: Most of these relationships are many-to-many and, as such, will need an XRef table to resolve this. Post back what you do with this and we can look at what's next.

CakePHP/SQL dynamic table creation & relations, I need a better approach

I've got a question about CakePHP and databases regarding an application idea I have. I'm still only at the first lines of code, but I'm trying to think out what it will require from CakePHP. One of the things I've realized is the structure of this application will require runtime table creation, which won't work because I need to bake.
Allow me to explain. There's a parent object called the list. The list has many submissions, it also has many comments. Basically users submit an item to the parent list, and they can also comment on the parent list. The things I'm having trouble figuring out is how I'd structure all of this.
My first thought was to have a table containing a row for each list, with it's basic properties. Then two tables per list containing lists submissions and comments. I'm not sure what kind of relationship the comments and submissions table should have to the row in the lists table containing the parent info, foreign key?
The problem with this approach is when a user creates a new list, this would require the creation of two new tables associated with that list. I don't see that this is possible in CakePHP due to needing a bake to generate the cake structure. My other thought was if Cake allowed me to access arbitrary tables without established models, I could access everything, but then I would have to define all the relationships at every point I rely on them.
I'm not really sure how to approach this, any help is appreciated.
You don't need to add new tables every time as you suggested above. You should have 3 tables, lists,list_submissions, and list_comments. When someone adds a list, you add a row to the lists table. list_submissions and list_comments should have a list_id field as a foreign key, so when a new comment or submission is added, just save the id of the list it belongs to in the appropriate table. Your ListSubmission and ListComment should have a 'belongs to' relationship with the List model.

Categories