Laravel - Query articles of a top category - eloquent

So i have categories, as an example, like this:
News (top category)
Sports (sub category)
World (sub category)
Local (sub category)
etc.
The articles all belong to some of the SUB CATEGORIES
(but dont have a record of belonging to the top category in the pivot).
article_id 23 -> category_id 8 (its a many to many, with a pivot table)
The articles also have published_at, status, hits fields.
Now I need to:
get Articles
that belong to any of the sub categories of the Top News category,
where('status', 1),
where('published_at', '<=', Carbon::now()),
orderBy('published_at', 'desc'),
I not sure if I am clear enough, i hope its enough.
Any suggestions?

Related

How to design many to many relation in mongodb?

The current scene is this:
There is a category table [category], the number of records is only more than 50, almost no increase, and the modification is rare.
There is a product table [product] currently millions of levels, will always increase.
These two are many-to-many relationships. One category will have more products, and each product will have multiple categories.
The category list is almost not changed, and there are about 1000 products in a category, and the list of a category will be changed not frequently.
Query requirements:
Query all categories (excluding the list of products under the category)
Query the category list by product_id
Query the product list by category_id
Operational requirements:
Modify the product list in category (add/delete a product to a category, sort the product list in a category, so the product list in category needs order.)
How many-to-many design of this kind of scene is better, there are some points:
1. If you follow the design of the SQL database, add a Category<-->Product relation table.
[Question] The order of each category of products is not well maintained. For example, the front-end performs a large-scale adjustment order on a category of products, and then requests it. The Category<-->Product relation table also needs to add an index field to indicate the order, and needs to update a lot of records. It is not particularly friendly to the operation requirements, is there any What can be optimized?
2. The way of NOSQL. Add a products:[] directly to the category to indicate a list of items in this category.
[Evaluation] In the query requirement, there is a requirement to query all categories (excluding the list of products under the category), which will pull out a lot of unnecessary data (products) at one time. Not applicable.
3. Add products:[] in the Category<-->Product association table
[Question] This can meet the operational requirements, but if you want to meet the Query requirments-2 [Query the category list by product_id], how to query it and will there be performance problems?
You need a third table (junction table) to complete the relationship. The keys must be primary keys along with a foreign key constraint.
tblProductCategories
PK product_id FK
PK category_id FK

DynamoDB modeling relational data (restaurant menus example)

I'm creating a web platform on AWS to enable restaurant owners in my community to create menus. I'm moving from a relational database model to a NoSQL solution and wondering the best way to organize this data. My current relational model is as follows:
Table 'restaurants': id (int / primary key), name, owner (int)
Table 'categories': id (int / primary key), restaurant (int), parent (int)
Table 'items': id (primary key), name, category (int)
Only the owner should be allowed to create/update/delete places, categories, and items.
What would you recommend as a de-normalized solution given the ownership constraint? I was thinking of doing the following:
Table 'restaurants': id (primary key), owner (sort key), categories (list of ids)
Table 'categories': id (primary key), restaurant (id), items (list of item objects), subcategories (list of category ids)
Wondering if it'd be better to have all category data contained within the restaurant table. As an example, a user should only be able to add an item to a category if they are the owner of the associated restaurant, which would take an additional query, per above.
Depends mostly how you use your data . If usually the Restaurant is read full, is ok to have all in the restaurants table.
If you have a lot of operations only on one category , for example many are interested only in food and not interested in drinks , then it would be good to have this done on categories.
I think for some restaurants would be better to have it split in categories and keep common data on restaurant level , address, phone , opening hours and so on .
I don't think write is important , seems to be over 90% read web site.0
Perhaps a cache solution ? Redis ? Memcache ? this would speed up even more.

Getting records through parentID from CategoryCollection in Typo3

I am using CategoryCollection to get the records of a specific category ID, but the problem is it only loads the exact category for e.g I have parent > child and I have attached child category ID to a record and I select child category, then it shows me the record fine, but if I select parentID, then it does not show the child category record.
$collection = \TYPO3\CMS\Frontend\Category\Collection\CategoryCollection::load(
$categoryID,
true,
'tx_myextension_table_name',
'categories'
);
Is there any built-in way to get the records of all child category if I select parent ID from CategoryCollectionor do I have to write something custom for that?
Unfortunately there is no built-in solution for complex selections like this. You will indeed need to write your own logic which could work like this:
Find categories whose parent is your category
Repeat this recursively for every category found until you don't find any children for each category anymore
Do a custom IN() query with the list of category UIDs
If you have deep category trees, the list of category UIDs could be put in a custom cache. You can use the root category UID or a hash thereof as key. These cache entries should be tagged with sys_category. Alternatively you can add a sys_category_<uid> tag for every category UID in your list. This ensures that whenever something changes about one of the categories, the cache entries are dropped and you can rebuild the list.

E-Commerce REST API for categories

In my e-commerce project, I am building a set of REST APIs to list the categories and products from the catalog management system. A category can have sub-categories and a product can belong to a category or stand by itself.
- root_category
- sub_category
- product1
- product2
- product3
Here in this example, root_category contains sub_category & product2. The subcategory contains product1. product3 doesn't belong to any category.
Here are the possible use cases:
List all the products in the system.
/products
Get product details by id.
/products/{id}
Search for products by id, name or description (need to query by one of these criteria)
/products?searchTem={searchTerm}&q=[byId|byName|byDescription]
List all categories in the system.
/categories
5(a). List sub categories belonging to a particular category.
/categories/{id}/categories
5(b). List root level categories. (eg. root_category).
/categories/#root/categories
6(a). List products belonging to a category.
/categories/{id}/products
6(b). List products that don't belong to any category. ie., root level products (eg. product3).
/categories/#root/products
Please let me know your comments and suggest improvements for these REST URIs listed above. I am a little concerned about 5(b) and 6(b).Can those URIs be designed in a better way.
I think that looks reasonable, the only issue I have is the use of category id.
I would prefer to see a category name used, which is more user-friendly.
Category name has to be unique obviously.

Core-Data Relationships

In my application there are N number of Users. Each user can have N number of Categories. Each category can have N number of Books.
I have created following Entities BookMetadata, Users, Categories
BookMetadata entity is a unique collection of books data from different users.
How do I go about creating relationship between these entities.
Relationship between - Users & Categories.(one to many) or users & books (one to many, one user can have one or more than one book).
Relationship between - Categories & Books.(one to many).
Is my understanding about implementing these relationships correct, I'm relatively new in creating relationships in CoreData. If no or a better solution, please let me know how to go about it.
EDIT:
Consider this scenario,
User 1 -> Category 1 -> Book1,Book2.
-> Category 2 -> Book3, Book4.
User 2 -> Category 1 -> Book1,Book2,Book3.
-> Category 3 -> Book4
Here In the above scenario. The same Book (Book 3) has different category for the different user.
How to handle this scenario?
EDIT 2:
I'm attaching a sample sqlite DB file #dropbox https://www.dropbox.com/s/nlmdojkk64bkf1b/test.sqlite.
As a convention Entities should be named as singular. The relationships should be named plural or singular relative to their relationship.
From the given information it is assumed that category is created by a user to group the books like a playlist or something. A category will be unique to user and it is not shared among users. A book can be included in any number of categories.
So this will become
User < -- >> Categories
Category <-- >> Books
Book < -- >> Categories
A relation should have two sides. Inverse let you define that. If a user can have any number of categories and a category can only have one user. Inverse of categories is a user.