How to get all information from a related collection in strapi v4? - strapi

I have created a collection (favorite) related to product and user.
My product collection has a field called images but when i make a GET request to favorites i dont get back the images. I have all permissions active for users. This is my query
http://192.168.0.102:1337/api/favorites?populate=product&filters[user][id][$eq]=4.
I am following: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest/filtering-locale-publication.html#filtering

Images are also considered to be 'relations' on a collection. You need explicitly tell it to populate those, see populating two levels: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest/populating-fields.html#relation-media-fields

Related

How to make an API endpoint in Laravel, that can be used to search any table we want?

Background
Let us consider a hypothetical scenario. Suppose we have five different tables in our database,
Customers
Categories
Products
Orders
OderDetails
Our client wants us to add a search bar to the frontend, where a user can search for a specific product and upon tapping the search button, matching products has to be displayed on the frontend.
My Approach for Tackling This Problem
In order to add the aforementioned functionality, I came across the following strategy.
πŸ‘‰ I would add an input box to ender the product name and a submit button.
πŸ‘‰ Upon pressing the submit button, a GET request will be sent to the backend. On the query parameters, the product name that has been entered by the user will be included
πŸ‘‰ Once the GET request is received to the backend, I will pass it down to the ProductsController and from within a method defined inside the ProductController, I will use the Product model to query the products table to see if there are any matching results.
πŸ‘‰ If there are any matching results, I will send them to the frontend inside a JSON object and if there aren't any matching results, I will set a success flag to false inside the JSON object and send it to the frontend
πŸ‘‰ In the frontend, if there are any matching results, I will display them on the screen. Else, I will display "No Results Found!"
Problem with My Approach
Everything works fine if we only want to search the products table. But what if, later our client tells us something like "Remember that search functionality you added for the products? I thought that functionality should be added to orders as well. I think as the list of orders by a user grows and grows, they should be able to search for their previous orders too."
πŸ‘‰ Now, since in our previous approach to search products was implemented in the ProductController using the Product model, when we are adding the same functionality to Orders, WE WOULD HAVE TO DO THE SAME THINGS WE DID IN THE ProductsController AGAIN INSIDE THE OrdersController USING THE Order model. It does not take seconds to understand that this leads to duplication of code.
Problem Summary
❓ How do we add an API endpoint in laravel for a search functionality that can be used to search any table in our database and get results, instead of the search functionality being scoped to a specific controller and a corresponding model?
A good start would be to create a trait called something like searchable and add it to all the models you want to search and put any shared logic between the different models in there. Possibly you'd want to manually allow different columns so in each model you have an array of searchable columns which you'd refer to in your trait.
Your controller would have to point to the related model and call a method in the trait that searches.
As others have pointed out this is a high level question so I won't go too much in detail. Of course there are a million ways to implement this, just try and keep shared logic in place.

How to fetch data from relationship in strapi.js?

I have created two content type builder which are category and sub-category respectively. While adding sub-category I had define one relationship which is one-to-many.
After creating successfully, I found that the basic CRUD API has been created and it works fine.
Now I need to find data like if I pass category-id then it has to return me it's all sub-category list.
Well, for this I can also write API manually, But I thought that strapi provides a feature of relationship though it may have some way to fetch data from the relationship table. In my app, I had set up a project with MySQL.
Expected output: Need a way to fetch data from a relation without writing custom API. Looking for inbuilt feature of strapi.
You have to use deep filtering.
πŸ“šHere is the documentation https://strapi.io/documentation/3.0.0-beta.x/guides/filters.html#deep-filtering
So you will be able to do /categories?sub-category.id=[your id]

Hide field rows when viewing a Laravel Nova Model

Is it possible to hide specific rows when viewing a model in Nova?
Let's say I have a bunch of Users and Companies. A User can own one or more Companies and should be able to view and edit them. But I don't want the other users to be able to see companies that they don't own.
I managed to remove the ability to edit, delete and see the name of a company if you don't own it but I can't seem to remove the companies from the list properly. (ScreenShot)
Is this even possible yet? Or do you have a better solution?
From the manual:
You can use these methods on each field:
hideFromIndex
hideFromDetail
hideWhenCreating
hideWhenUpdating
onlyOnIndex
onlyOnDetail
onlyOnForms
exceptOnForms
Example:
Text::make('Name')->hideFromIndex()

How do I store static data in Laravel?

In our application we will give titles to users based on their points. So, if a user has 10-99 points, that user might get the "Novice" title, but a user with 100-199 points might get the "Regular User" title. I plan on eager loading a user's points using an attribute and relationship, and once I have those points I will use an attribute method to assign the title.
But how do I get the list of possible titles?
I could make a model, a migration, and a seed file, but I feel like these titles won't change much and certainly would never need to be updated in an API call. I could also hardcode an array of points and titles and do a quick lookup to see which title belongs to a user, but then I need to somehow deliver those titles to the user in an Attribute method. Or I could store them in a repository or the cache.
Can I access a repository from within a model? Is it better to store this sort of data in a DB anyways, regardless of how often it's updated or queried?
You could use entries in your .env file to store the entries and then use some logic in your php to select the correct .env entry.
LEVEL_1_TITLE=Novice
LEVEL_2_TITLE=Regular
...
if($user->points < 99){
$title = env('LEVEL_1_TITLE');
}
...
Or do the same thing from an array in a class that you create and just select the correct array entry based on the points.

Complex db Queries in MVC

I am trying to do an oddball type of query to a database that I cannot seem to figure out how to do.
I made a sample database, shown below, just for this question, as I cannot display the real database because of its proprietary nature.
http://i.stack.imgur.com/DGDaQ.png
My goal is to be able to add an order and order items on the same page, while displaying a list of the products as shown below.
http://i.stack.imgur.com/QLMrB.jpg
Not every order will contain all of the products, but I still need to be able to list all of the products and have the ability to enter a quantity if there is one. How would I go about laying this out using MVC? Currently, I am using a ViewModel inside the Create View for the Orders to display the ProductName list. I cannot seem to figure out how to get boxes that would allow me to add in the OrderItems information for each product name.

Resources