How to store different type and number of fields in one database table? - laravel

Hello everybody I'm making a "Bulletin board", like this: http://stena.kg/ad/post, I'm using Laravel 5.0, and don't know how to store different fields in database table, for example if I choose "Cars" category I should to fill Mark, Model, Fuel (etc fields for cars category), If I choose Flats category I should fill fields like Area, Number of rooms etc...How to organize all of this? I tried some ideas but nothing helped me(

Try to save data as json in table. Parse json format to string and save it in db, but it will cause many problems in future, so not recommend that solution. I recommend to store data in separate tabels, each one for category. For optimise process it is possible to create catregory table, and category_item table with fields like name, description and so on. Different category demands sp=ecific fields, so best solution is to create table per category.

Related

Laravel models, database and pivot tables question

Hello I am working with Laravel,
I have to create two simple models, let's say Stores and Books.
Stores can have one or multiple Books and Books can belong to many Stores.
Of course I will use a many to many relationship, with a pivot table.
Books the can have different prices depending the store.
I think a separate table can only complicate things, in my mind the pivot table associating books and stores should have a price column, but pivot tables only contains store_id and book_id.
Should I create a book_prices and associate it with books and to stores? What is the best approach?
You are free and able to set other attributes on your pivot table. You can read more about it in the docs.
https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns
You have to define the relationship accordingly, the following should clarify how this works. In this example you use the many-to-many relationship and add the price column to every retrieved pivot model.
public function books()
{
return $this->belongsToMany(Book::class)
->withPivot('price')
}
For example, you are able to access the pivot column in a loop like this
foreach ($shop->books as $book)
{
echo $book->pivot->price;
}
You can define additional columns for your pivot table in the migration for the pivot table, and then when defining the relationship use withPivot to define the additional columns so they come through in the model:
return $this->belongsToMany(Book::class)->withPivot('price');
(Adapted from the Laravel documentation, see https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns)
Depends on the complexity of your case, but yes, you have two options for it. Let's say that the pivot table is called as book_store:
Directly adds price column to book_store. This is obviously the simpler option. The drawbacks are:
The history of the price changes isn't logged. You'll have to create another table for logging if you want to keep this history information.
Changes made to price will directly change the price of the related book_store record. Meaning that a price is being updated "live" e.g users cannot update the price now but "publish" it some time later just like this example in the doc.
Create a new, different table to store the price. This may seems relatively more complex, but it may also be more future-proof.
Basically, you get 2 things that you miss in the first option above.
Don't think too much about book_store being a pivot table. One way to see it is like this: book_store IS a pivot table from books and stores tables viewpoints, but it's also just a normal SQL table which could relate to any other tables using any kind of relationships.
If you want to implement this, make sure to create a primary-key in the book_store table.
Alast, it all depends on what you need. Feel free to ask if you need more insight about this. I hope this helps.

How to access categories in Wix Database

I've been struggling to create a repeater which accesses specific information in my Wix data base. My question is a bit more complex than connecting the repeater to a column.
Rather, I want the repeater to access types of data within a column on the database. I have a column in the data base with an id, "category". For illustrative purposes, say the name of the data base is "store". I'm selling two different types of shirts. Some are casual, others formal. "category" has twenty iterations of both "formal" and "casual". If I create a repeater which accesses "category" and displays its text, I'll end up with a repeater forty iterations long. Instead, I want to parse out how many types of categories there are (in this case, two: "formal" and "casual") and only to display each category once-- a repeater which is only two iterations long.
(I know Wix has the ability of accessing how many types of information there are in column "category", I just don't know how to actualize on that ability. I know Wix has this ability because you can create a dynamic item page with a url of name "category". This will create a page for each category in "category". How do I do the same thing but for a repeater?)
One way to accomplish this is by using the Wix Data API Distinct query and then using the query result as the data for your repeater. This means you would not use the GUI connect to dataset, but the code in the page's IDE. Once the query returns, you can set the data property of the Repeater and then use the onItemReady() function for any further manipulation you may need.

Inserting Dynamic Data Into Cassandra Column Family

It might not be a relevant title for this question, but if I explain what I mean, it makes sense.
In order to learn how Cassandra works, I have the following scenario :
Consider I have an online store with lots of different products such as cars, smartphones, clothes etc. in which every product has its own specs.
I need some examples around how to model my Products column family?
It should be mentioned that I need to filter them by specs. something like:
SELECT * FROM Products WHERE Ram > 3;
Cassandra data modeling suggests one table for each query.
Now if you want to query on specs, we need to make specs as one of the keys. Using that table you can query something like this:
SELECT * from products where specs = 'RAM';
If you want to filter on brand, you need to keep brand_name as one of the columns and your query would look like this:
SELECT * from from products where brand_name = 'brandname';
So, what we are looking at here is we need to make 'RAM' as one of the columns.
You would need to do data wrangling to create data for each specs. For example: you have data with product_id,spec,inventory. You would need to do some analysis and see if you need to create columns for each spec or you can group a lot of specs and create new columns. The final data may look like :
product_id,ram product_id,hdd etc. You get the idea.
If you have a lot of specs, then maybe you need to create separate table for different products and then design data model from there.
I'd suggest you take data modeling course for a better understanding of cassandra data modeling.
I'm not quite sure if this is the best answer. However after some researches I found THIS.
This way I can store Product's specs in a column with MAP<TEXT, TEXT> data type. Then create INDEX on it.
Then I can query it like the following :
SELECT * FROM Products WHERE Specs['Ram'] > '3GB';
Hence, the Ram is a Key in the MAP and 3GB is it's value.

Modeling many-to-many relationship in data warehouse

I have to design data warehouse model and ETL process for class at my University. My data warehouse has to store opinions / comments about a product, each record should consist of:
comment text (String)
product score ({0, 0.5, … , 4.5, 5})
comment author (String)
comment date (Date)
product recommendation ({Yes, No})
comment up votes (Int)
comment down votes (Int)
product pros (many Strings, e.g {price, design, durability, … }) and its count
product cons (many Strings, e.g {too loud, too heavy, price, … }) and
its count
In addition data warehouse should store information about product:
product category
product brand
product model
I want to create data warehouse model first, but I have problem with storing product pros and cons as it is many-to-many relationship. In normal relational database I would simply create associative table, but here I am not sure how to proceed, after all I don’t want to normalize facts table.
I am considering 3 approaches, first, which I presented in diagram below. I used bridge table method (though, I don’t know if correctly) to get rid of many-to-many relationship. I don’t know how it will impact querying performance.
Second approach I may use is boolean column method. In PROS and CONS table I can create a column for each possible value, but there can be up to 100 different pros or cons. Also number of possible pros or cons is not constant in time. Authors in their comments can list new pros or cons (that’s how it works in data source), but I can’t add new columns (I shouldn’t change data in data warehouse).
Third approach I am considering, is to keep pros in PROS table but in 1 column, where values will be separated using commas or some other delimiter e.g. “price, design, color”. It keeps things simple but hard to analyze or slice & dice.
Which approach should I use in this situation? Which is better for loading data into data warehouse, because form data source I will get all the comments and I want to only load comments that are new since last loading?
What I think is, if we can get your first option little bit modified to than what you have said here, it would be the best as I understand.
in your image you have provided, having the Pros_Bridge_Detail table is fine. The rest need to be changed.
you can remove the pros_Bridge table that holds just the count. you can actually add that column to your COMMENT fact table you have up there. That would be more efficient and easy when it comes to queries rather than querying in many tables.
you said you have many areas to give pros like price, design, durability etc. Lets put those stuff into a separate dimension.
Add a new column to your Pros_Bridge_Detail table to hold the ID of the newly created Dimension that holds the product pro types (Design, durability etc).
Now, once you add a product Pro, the Pros_Bridge_Detail table will have the pros the user give and also hold the value of regarding what the pro is given via the ID of the new dimension.
Also don't forget to store the Comment ID as well in Pros_Bridge_Detail table as that will be your link (FK) to Comments fact table you have.
Same can be done to Cons as well.
Hope you understand what I just explained and hope it helps. let know if you have any issues.

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!

Resources