Skip to content

Instantly share code, notes, and snippets.

@Shahinyanm
Created March 17, 2023 19:30
Show Gist options
  • Select an option

  • Save Shahinyanm/21de0cd6f02e637bf07bd37e1cc643c8 to your computer and use it in GitHub Desktop.

Select an option

Save Shahinyanm/21de0cd6f02e637bf07bd37e1cc643c8 to your computer and use it in GitHub Desktop.
CodeRefactor
Version one )))
UntrackedItem::query()
->groupBy(function ($untrackedItem) {
return $untrackedItem->properties
->sortBy('property_value_id')
->pluck('property_value_id')
->join('.');
})
->havingRaw('COUNT(*) > 1')
->each(function ($duplicatesGroup) {
$lastItem = $duplicatesGroup->pop();
$duplicatesGroup->each(function ($item) use ($lastItem) {
$lastItem->available += $item->available;
$lastItem->unavailable += $item->unavailable;
$lastItem->checked_out += $item->checked_out;
$item->history()->delete();
$item->delete();
});
$lastItem->save();
});
/ First, let's assume that the $untrackedItems variable holds a collection of UntrackedItem models.
// We can group the UntrackedItems by their property values and sort them by property_value_id.
$groupedItems = $untrackedItems->groupBy(function (UntrackedItem $untrackedItem) {
return $untrackedItem->properties
->sortBy('property_value_id')
->map->property_value_id
->join('.');
});
// Then, we can loop through each group of duplicates.
$groupedItems->each(function ($duplicatesGroup) {
if ($duplicatesGroup->count() < 2) {
return;
}
// We can get the last item in the duplicates group and update its available, unavailable, and checked_out properties.
$lastItem = $duplicatesGroup->pop();
$lastItem->available += $duplicatesGroup->sum('available');
$lastItem->unavailable += $duplicatesGroup->sum('unavailable');
$lastItem->checked_out += $duplicatesGroup->sum('checked_out');
// We can loop through the rest of the duplicates and delete their history and then delete them.
$duplicatesGroup->each(function ($item) {
$item->history()->delete();
$item->delete();
});
// Finally, we can save the updated last item.
$lastItem->save();
});
1. When do we need to use key attribute and what for?
```In Vue, the key attribute is used to help Vue efficiently update the DOM when it re-renders a component. It is used to hint to Vue which components or elements have changed, been added, or been removed, so that Vue can apply the appropriate updates without needing to re-render everything.
Here are some scenarios when you might want to use the key attribute in Vue:
When rendering lists: When you use v-for to render a list of items, Vue needs to track which item is which. If the order or contents of the list can change, you should use the key attribute to give each item a unique identifier. This helps Vue determine which items have changed, and update only those items in the DOM.
When conditionally rendering elements: When using v-if or v-show to conditionally render elements, you may need to use the key attribute to avoid reusing the same element across multiple conditions. This can prevent unexpected behavior or rendering issues.
When reusing components: When reusing components with similar content or functionality, you may need to use the key attribute to differentiate between them. This can help Vue render the correct component with the correct state.
In general, the key attribute should be used whenever you are rendering dynamic content that can change over time, and when you want to optimize Vue's rendering and update process. However, it is important to note that overuse of the key attribute can also lead to performance issues, so it should be used judiciously and only when necessary.```
2. How do we set a props default value if the prop&#39;s type is Array or Object, and why?
props: {
user: {
type: Object,
default: function () {
return {
name: 'John Doe',
age: 30
};
}
},
users: {
type: Array,
default: function () {
return [
{
name: 'John Doe',
age: 30
},
{
name: 'John Doe',
age: 30
},
];
}
}
}
3. Can we watch for an object property changes?
<template>
<div>
<p>My name is {{ person.name }}</p>
<button @click="changeName">Change Name</button>
</div>
</template>
<script>
export default {
data() {
return {
person: {
name: 'John Doe',
age: 30
}
}
},
watch: {
'person.name': function(newName, oldName) {
console.log(`Name changed from ${oldName} to ${newName}`);
},
//////////////If Object have another nested object, and we are using vue 2, we need to use another syntax//////
person:{
handler(newValue,oldValue) {
console.log(`Name changed from ${oldName} to ${newName}`);
},
deep: true
},
},
methods: {
changeName() {
this.person.name = 'Jane Doe';
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment