Laravel 7 Order Query By Appended field - laravel-7

So i have a posts model and a comments model.
In posts model i have 3 attributes:
getCurrentMonthCommentsAttribute
getCurrentYearCommentsAttribute
getTotalCommentsAttribute
Each of these filters the relationship by the created_at field and returns count().
How do I order by these fields in a query and paginate results?
Posts model has fields: id, name, description, created_at, modified_at
Comments model has fields: id, post_id, content, created_at, modified_at
I don't even know how to begin.

Related

Product schema:- ProductId:string Name:string Description:string TagSpecial:boolean Rating:number Tagging:[{ year:number, tagId:number }]

Product schema:-
ProductId:string
Name:string
Description:string
TagSpecial:boolean
Rating:number
Tagging:[{ year:number, tagId:number }]
You have given a productId and Write a single MongoDB query to find the count of tagSpecial and maximum value of rating, which have tagSpecial true and given productId is not included.
You have given a productId, tagging year and tagId, write a mongoDB query to update tagging of given productId.
You have given a productId and tagging year, write a mongoDB query to remove tagging which is greater than the given year.
Product schema:-
ProductId:string
Name:string
Description:string
TagSpecial:boolean
Rating:number
Tagging:[{ year:number, tagId:number }]

Sequelize Select models referenced in parent/child tables

If I have the following tables
Post - id, title, content, categoryId
Category - id, name
And some categories don't have posts linked to them, how can I select only the categories that are referenced from the Posts table
I found this in their github issues, but its code from V1, which no longer works, however it demonstrates, I think, what I would like to accomplish
ModelA.findAll({
include: [ModelB],
having: 'count(ModelB.id) > 0'
});
If ModelB is referenced one or more times in ModelA, include it.
Edit: I don't really want to include it, as I only need the data from the Category table, and not the Posts table.
I'm using postgres, if it matters.
required tag helps you in this.
ModelA.findAll({
include: [{
model: ModelB,
required: true
]
});
This will only return rows in ModelA which have at least one corresponding entry in ModelB

Sequelize include with two where condition from two tables

I am a new one working in Sequelize. I do not know how to change the query to sequelize
SELECT a.* FROM employees a,emp_dept_details b where b.Dept_Id=2 AND a.Emp_No = b.Emp_Id
Emp_No is Primary Key in Employee table
EmpDeptDetails.hasMany(Emp, {foreignKey: 'Emp_No'})
Emp.belongsTo(EmpDeptDetails, {foreignKey: 'Emp_No'})
I am not sure but try out the below query
Assuming
You have the relationships between the 2 tables defined in the models class.
You have Model Employee for employees table and EmployeeDeptDetails for emp_dept_details table. Change them to your respective names before trying.
Employee.findAll({
include: [{
model: EmployeeDeptDetails,
where: { Emp_Id: Sequelize.col('employees.Emp_No'), Dept_Id: 2 }
}]
});

Sequelize hasOne through another table

I have the following three models
User
user_id
user_email
Group
group_id
group_name
GroupUser
group_user_id
user_id
group_id
How can I get group details(if it mapped with user ) while fetch User data ?
Is there any Sequelize hasOne association through another table ?
There is currently no direct way in sequelize for what you are asking
ref https://github.com/sequelize/sequelize/issues/3845
but you can do
Group.belongsTo(GroupUser)
User.belongsTo(GroupUser)
GroupUser.hasOne(Group)
GroupUser.hasOne(User)
and do nested include (but this is not very efficient)
User.findAll({include : [{model:GroupUser, include :[{model : Group}]}]})

ordering results using mongoose in express

I'm using mongoose to create and model my document schemas.
I have a user_id attribute on a bunch of different schemas, along with a created_at date attribute on them.
I want to generate a list, ordered by created_at date for display as a feed of recent activity on the user's homepage.
How would I query several different models, and then order these items into an array that I can then pass to my ejs view?
You can sort your mongoose query using the sort function. Here are two examples:
query.sort({ field: 'asc', test: -1 });
or
Person
.find({user_id: 123456})
.sort('-created_at')
.exec(function(err, person) {
// push these results to your array and start another query on a different schema
});

Resources