I am newbie in Laravel. I am using laravel as a REST End Point, I use Eloquent: API Resources for transforming my data to JSON.
I am using Collection and Resource to parse my data came from my query. So below is the structure of my response:
data:
0
id
name
1
id
name
Now I need to have some categorization in the data, hence I require the data structure to be in below format
data:
24H
0
id
name
1
id
name
7D
0
id
name
1
id
name
I tried couple of ways to accomplish this, the static way I tried for testing was to change the ResourceCollection's toArray method and append the key 24H for the data we get.
But I know that is not correct and generic way.
I would like to know how I can achieve the above response format in generic and extensible manner.
Your help is much appreciated.
Thanks.
Laravel Collections have a groupBy method that may help you here, make sure the column you want to use as the key is selected as well. So if you have
data:
0:
id: 1
name: Bob
category: 24H
1:
id: 2
name: Bill
category: 7D
Then $data->groupBy('category') would return the following
data:
24H:
0:
id: 1
name: Bob
category: 24H
7D:
0:
id: 2
name: Bill
category: 7D
Related
I have three tables:
PhaseTasks
phaseId
taskId
1
1
1
2
2
4
Tasks
taskId
taskName
defaultResource
1
Do a thing
1
2
Do another thing
1
3
do some other thing
2
resources
id
name
1
Engineering
2
Support
2
Sales
I am trying to get a result set where i can print
Phase Id: 1, TaskName: Do a thing, ResourceName: Engineering
Phase Id: 1, TaskName: Do another thing, ResourceName: Engineering
Here is my phaseTasks Model:
public function phaseTasks(){
return $this->hasManyThrough(
resources::class,
tasks::class,
'id',
'id',
'taskId',
'defaultResource'
);
}
and my controller:
$phaseData = phaseTasks::with('phaseTasks')->where('phaseId','1')->get();
i cant get the task table info, nor can i figure out how to access the resource data. I DO see the resource data in the print_r($phaseData), i do not see the tasks stuff, im guessing i need to get into pivots for that part.
***EDIT
I was able to figure out how to get the resources.name entity
$phaseData[0]->phaseTasks[0]->name;
now i just need to figure out how to get the taskName entity.
I'm not sure but I think you got the relationship the other way around: it's not hasmany (through) but belongs to.
So, phasetasks - task_id belongs to a task - default resource belongs to a resource
And your phasetasks looks like a pivot table in itself, but that may be ok.
So if you agree with my assumption and create the relations my way, try something like
PhaseTask::with('task.resource')->get();
I'm using Amplify from AWS to build a small ecommerce project using React as frontend.
I'd like to know how I should write the "Product" and "Order" types in the schema in order to be able to write productId's to a product array in the Order table when users complete a purchase.
My schema.graphql file:
type Product #model {
id: ID!
name: String!
price: Int!
category: String!
images: [String]!
}
type Order #model {
id: ID!
products: [Product] #connection
}
My question is about the last line, do I need to define that [Product] connection there or I can use [String] to store product id's in a simple string array?
Point 1: In dynamoDB, you only need to define the data type of your partition key and sort key, and these can be string, number etc. For all the other attributes, you don't need to define anything.
Point 2: The dynamoDB designers prefer using a single table per application, unless it's impossible to manage data without multiple tables. Keeping this in mind, your table can be something like this.
Please observe: Only Id aka partition key and Sk aka sort key column is fixed here, all other columns can be anything per item. This is the beauty of DynamoDB. Refer to this document for dynamoDB supported data types.
I have a database with entries which I can fetch using ActiveRecord. Currently, using something like post.to_yaml yields:
!ruby/object:Post
concise_attributes:
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: id
value_before_type_cast: 1
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: user
value_before_type_cast: efy5qC5YmJNml23JowOUrlmfN0D2
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: content
value_before_type_cast: bol4
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: location
value_before_type_cast: '123'
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: timestamp
value_before_type_cast: '12:00'
new_record: false
The exact collection i'm returning is as follow: record = Post.order(:timestamp).offset(15 * 0).first(15)
This returned result contains several fields which will be returned to a Flutter application. The data will populate a widget with several fields such as content, date and location, all of which is returned by the above query.
I could use a Dart library to parse the YAML, but is there a better way to condense the returned values so that only the necessary fields are shown?
As per the description shared it seems like you have data from the database and you now need to select only particular fields that needs to be shown.
As per current scenario you could use something like:
post.as_json(only: [:content, :name, :location])
Else you could modify the query you are using by using select statement for selecting specific attributes from database.
Post.select(:name, :content, :location)
Hope it helps!!
I'm working on a rails 2 project. I'm trying to fetch a record from tags table by using find_by_* . Its giving different result.
May I know why is this working like this?
In my model:
existing = user.tags.find_by_name(tag)
in Log:
SELECT * FROM `tags` WHERE (`tags`.`name` = 'Ror') AND (`tags`.user_id = 1) LIMIT 1
RuntimeError (#<Tag id: 980191043, user_id: 1, name: "rOr", created_at: "2014-09-09 12:18:55", updated_at: "2014-09-09 12:18:55">):
Are you using MySQL?
If so it is likely it is doing a case insensitive comparison. Whether MySQL is case sensitive is based around the field collation of the column: http://dev.mysql.com/doc/refman/5.0/en/charset-column.html
I have a site that is for a video game I play and am working on improving the performance of the site by implementing some additional caching. I've already been able to implement query result caching on custom repository functions, but haven't been able to find anywhere that explains how I can include query result caching on the built in functions (findOneById, etc). I'm interested in doing this because many of my database queries are executed from these 'native' repository functions.
So as an example I have a character entity object with the following properties: id, name, race, class, etc.
Race and class in this object are references to other entity objects for race and class.
When I load a character for display I get the character by name (findOneByName) and then in my template I display the character's race/class by $characterObject->getRace()->getName(). These method calls in the template result in a query being run on my Race/Class entity tables fetching the entity by id (findOneById I assume).
I've attempted to create my own findOneById function in the repository, but it is not called under these circumstances.
How can I setup doctrine/symfony such that these query results are cache-able?
I am running Symfony 2.1.3 and doctrine 2.3.x
I've found out that it isn't possible to enable query cache on doctrine build in functions. I will post a link which explains why later after I find it again.
Your entities probably look something like this:
MyBundle\Entity\Character:
type: entity
table: Character
fields:
id:
id: true
type: bigint
name:
type: string
length: 255
manyToOne:
race:
targetEntity: Race
joinColumns:
raceId:
referencedColumnName: id
MyBundle\Entity\Race:
type: entity
table: Race
fields:
id:
id: true
type: bigint
name:
type: string
length: 255
oneToMany:
characters:
targetEntity: Character
mappedBy: race
If that's the case, then modify your Character entity mapping so that it eagerly loads the Race entity as well:
MyBundle\Entity\Character:
...
manyToOne:
race:
targetEntity: Race
joinColumns:
raceId:
referencedColumnName: id
fetch: EAGER
Doctrine documentation on the fetch option: #ManyToOne