Select from table where both columns have same value - database

I have the code that selects all articles. What I am doing is, I think, wrong. I am selecting all values and then I am executing them.
Article::all()->each(function($article) {
if ($article->created_at == $article->published_at && $article>published) {
$article->update(['published_at' => null]);
}
});
What I need to do is select only those articles that have the mentioned created_at and updated_at the same.
How could I do that on the database level?
I need sth like that:
Article::where('created_at', '=', 'published_at')->where('published')->get()->each(function($article) {
$article->update(['published_at' => null]);
}
});
This code sure does not work, its just for imagine.

edit your code
Article::all()->each(function($article) {
if ($article->created_at == $article->published_at && $article->published) {
$article->update(['published_at' => null]);
}
});

You only need to search on the two columns for the one value:
Article::where(['created_at' => $date, 'updated_at' => $date ])->get();
Edit:
Use whereRaw for this
Article::whereRaw('created_at = updated_at')->get();

Related

How to sort the columns in the below code

How to filter the columns? Here I need a date filter column, When we click the header of the date, it automatically sorts the column.
Actually, they asked like this "Need to sort by date and stick the 6 newest on this one as well"
Can you please help with this?
Here is the code I used.
You need to use a helper function in order to parse the string to Date object.
Then you can apply the sorting
Check the code snippet.
I store the Date object into another property in order not to break anything. You can of course replace it with the current property and format it accordingly inside your table cell.
function parseStr(str) {
let parts =str.substring(0, 10).split('.');
return new Date(parts[2], parts[1] - 1, parts[0]);
}
let array = [
{
"number":"5421787",
"amount":1391.74,
"duedate":"28.08.2020 00.00.00",
"voucherdate":"14.08.2020 00.00.00",
"invoicelink":"",
"status":"Betalt"
},
{
"number":"5499047",
"amount":499.0,
"duedate":"29.09.2020 00.00.00",
"voucherdate":"15.09.2020 00.00.00",
"invoicelink":"",
"status":"Betalt"
},
{
"number":"5574780",
"amount":499.0,
"duedate":"29.10.2020 00.00.00",
"voucherdate":"15.10.2020 00.00.00",
"invoicelink":"",
"status":"Betalt"
}
]
console.log(array.map(obj => ({ ...obj, voucherdate2: parseStr(obj.voucherdate)})).sort((a,b) => b.voucherdate2.getTime() - a.voucherdate2.getTime()))
So in your code
const invoiceData = useSelector((state) => state.userReducer.invoices)
let sortableInvoiceData = invoiceData.map(obj => ({ ...obj, voucherdate2: parseStr(obj.voucherdate)})).sort((a,b) => b.voucherdate2.getTime() - a.voucherdate2.getTime())
const renderInvoices = sortableInvoiceData.map((invoice, index) => {

How to make $el.text() into number type?

I got this code in cypress to get only numbers in the table.
cy.get('.MuiTable-root > .MuiTableBody-root > .MuiTableRow-root > .MuiTableCell-root:first-child')
.each(($el, $index) => {
if ($el.text() !== 'REDHAIR')
values.push($el.text())
expect(values.parseInt()).to.be.within(1,3)
But there is an error it said that parseInt() has no property in values.
cy.log has this result
log 1
log 2
log 3
Thanks.
I should be I am pretty sure
parseInt(values)
I found the answer. Thanks for the idea.
if ($el.text() !== REDHAIR')
values.push($el.text())
items = values.map(value => parseInt(value)) cy.log('Items', items)
}).then(() => {
items.forEach((value) => {
expect(value).to.be.within(1,3)
}
}
This will iterate the numbers inside an array.

Filter Firestore with multiple where not working

I have this docs
let wall = firebase.wallCollection;
then I want to filter it with multiple where:
location (location == filterLocation)
price (price < filterMaximumPrice)
This is my filter methods on Vue
filterResult(){
let self = this;
if(self.filterLocation!=""){
wall.where('location','==', self.filterLocation);
console.log("Location flag");
}
if(parseInt(self.filterMaximumPrice)!=0){
dinding.where('price','<', parseInt(self.filterMaximumPrice));
console.log("Price flag");
}
wall.get()
.then(snapshots => {
snapshots.forEach(doc => {
self.listFilteredWall.push(doc.data());
}, this);
})
}
The problem is that 2 where function not working and still give all wall output without filter.
How to fix this?
CollectionReference extends Query. The result of where() is a new Query:
Creates a new query that returns only documents that include the
specified fields and where the values satisfy the constraints
provided.
You need to retain the result Query if each where() and use it for the get(). Something like this:
filterResult(){
let self = this;
let query = wall;
if(self.filterLocation!=""){
query = query.where('location','==', self.filterLocation);
console.log("Location flag");
}
if(parseInt(self.filterMaximumPrice)!=0){
query = query.where('price','<', parseInt(self.filterMaximumPrice));
console.log("Price flag");
}
query.get()
.then(snapshots => {
snapshots.forEach(doc => {
self.listFilteredWall.push(doc.data());
}, this);
})
}

Using 'chunks' together with 'with' on Laravel query builder

dunno if this is possible at all.. I'm trying to query a large set of data with relations like so:
Parent::with([
'child' => function($query) {
$query->('published', '=', true);
$query->with('child.of.child', 'some.other.child');
$query->chunk(400, function($childs) {
// how is it now possible to add the $childs to the parent result??
});
}
]);
$parent = [];
Parent::with(
[
'childs' => function ($query) use (&$parent) {
$query->where('STATUS', '!=', 'DELETED');
$query->with('some.child', 'some.other.child');
$parent['models'] = $query->getParent()
->getModels();
$query->chunk(
400, function ($result) use ($query, &parent) {
$query->match($parent['models'], $result, 'child-relations-name');
});
}
])
->get();
Now $parent['models'] contains the tree with all the nested child relations... Dunno if this is the smartest way to do so, but it works for now.

Query for most favorited in each category

I have a table of creations. Each belongs to a category (with categoryId). They also have a field called statFavorites.
I want to return a flat list of the single creation with the most favorites for each category in a list.
The only way I can think of doing it is with groupedMapReduce. Is there another way?
var categories; // objects with an id
r.table('creations')
.filter(function(creation) {
return categories.filter(function(category) {
return category.id == creation.categoryId
}).length > 0
})
.groupedMapReduce(function(row) {
return row("categoryId")
}
, function(row) { return row }
, function(best, creation) {
return r.branch(creation("statFavorites").gt(best("statFavorites")), creation, best
})
Two things happening above: First, I'm filtering the creations to only match the categories I care about (equivalent of an in query in mongo. How to do this in rethink?)
Second, I'm getting the most favorited of each one.
Is there a better way of doing this? It may also be ok to pre-calculate things when I'm writing data.
You can do something like this:
The equivalent of in is contains
http://www.rethinkdb.com/api/javascript/contains/
categories = [id1, id2, id3]
r.table('creations')
.filter(function(creation) {
return r.expr(categories).contains(creation("id"))
})
.groupedMapReduce(function(row) {
return row("categoryId")
}
, function(row) { return row }
, function(best, creation) {
return r.branch(creation("statFavorites").gt(best("statFavorites")), creation, best
})
If categories is an object like
{
id1: true,
id2: true
}
You can also use hasFields instead of contains
http://www.rethinkdb.com/api/javascript/has_fields/
r.table('creations')
.filter(function(creation) {
return r.expr(categories).hasFields(creation("id"))
})
.groupedMapReduce(function(row) {
return row("categoryId")
}
, function(row) { return row }
, function(best, creation) {
return r.branch(creation("statFavorites").gt(best("statFavorites")), creation, best
})

Resources