Using a POST on my front-end (React.js, with react-beautiful-dnd), I'm trying to update the position of the items a user has stored.
My strategy is to send over an updated index of IDs that are ordered in the specified position.
My Data (example, simplified)
┌────┬────────┬───────┐
│ id │ title │ index │ <── my "position"
├────┼────────┼───────┤
│ 1 │ Apple │ 4 │
│ 2 │ Banana │ 1 │
│ 3 │ Mango │ 3 │
│ 4 │ Kiwi │ 2 │
│ 5 │ Orange │ 0 │
└────┴────────┴───────┘
New positions array (anatomy)
$value: sorted ids ------> [3, 2, 5, 1, 4]
: : : : :
$key: new index will be… 0, 1, 2, 3, 4
The Code
The controller ingests an array that derives the new positions from the keys of the array. It loops then through the collection of given IDs and reassigns them the updated index.
public function reorder(Request $request) {
$newIndex = $request->newIndex;
$items = Item::whereIn('id', $newIndex)->get();
foreach ($newIndex as $key => $value) {
// TODO: check if $item->index needs to be rewritten at all
$item = $items->find($value);
$item->index = $key;
$item->save();
}
return response('Success', 200);
}
dd() of $request->input
array:1 [
"newIndex" => array:5 [
0 => 4
1 => 1
2 => 2
3 => 5
4 => 3
]
]
The problem
It seems… the code only works once? Did I miss something? The POST fires every time and the set array is accurate, so the issue is not on the frontend side of things.
Related
I got this array:
What I want to do next is to make a collection from the array and I write:
$variant_images = collect($p->images);
but I got the error:
"Undefined index: images"
What's bad in my code and how to solve it?
Assuming $p is array like this
$p = [
'images' => [
1, 2, 3, 4
]
];
you can write
$collection = collect($p);
// access the images
var_dump($collection->get('images'));
and the output would be
array:4 [▼
0 => 1
1 => 2
2 => 3
3 => 4
]
I am trying to load JSON data from Kafka into Clickhouse where the JSON contains some escape sequences. E.g:
:) SELECT JSONExtractRaw('{"message": "Hello \"to\" you!"}', 'message')
SELECT JSONExtractRaw('{"message": "Hello "to" you!"}', 'message')
┌─JSONExtractRaw('{"message": "Hello "to" you!"}', 'message')─┐
│ "Hello " │
└─────────────────────────────────────────────────────────────┘
It appears that prior to calling JSONExtractRaw, the input strings are unescaped, which produces invalid JSON. The unescaping seems to be reproducible with this minimal example:
:) SELECT 'Hello \"there\"'
SELECT 'Hello "there"'
┌─'Hello "there"'─┐
│ Hello "there" │
└─────────────────┘
I am wondering if it's possible to retain the original (escaped) representation of the input.
Thank you!
SELECT '{"message": "Hello \\"to\\" you!"}'
┌─'{"message": "Hello \\"to\\" you!"}'─┐
│ {"message": "Hello \"to\" you!"} │
└──────────────────────────────────────┘
Let's test your case without CLI to emulate the table filled by data coming outside:
echo '{"message": "Hello \"to\" you!"}' | clickhouse-client \
--password 12345 \
--query="INSERT INTO test.json_001 SELECT json FROM input('json String') FORMAT CSV"
create database test;
create table test.json_001 (json String) Engine=Memory;
Get required data:
clickhouse-client \
--password 12345 \
--query="SELECT JSONExtractRaw(json, 'message'), JSONExtractString(json, 'message') FROM test.json_001"
# result:
# "Hello \\"to\\" you!" Hello "to" you!
SELECT
JSONExtractRaw(json, 'message'),
JSONExtractString(json, 'message')
FROM test.json_001
/* result
┌─JSONExtractRaw(json, 'message')─┬─JSONExtractString(json, 'message')─┐
│ "Hello \"to\" you!" │ Hello "to" you! │
└─────────────────────────────────┴────────────────────────────────────┘
*/
It works as it should. I don't see any problems.
I'm new to Jekyll and Liquid, and I've been trying to utilize a site that has 100+ pages, separated into 15 modules. I need the navigation to reflect only to related pages, but I'm stuck on how to do this efficiently. For example, a directory like:
animal-species/
├── _docs/
│ ├── mammals/
│ │ ├── mam-page01.md
│ │ └── mam-page02.md
│ ├── reptiles/
│ │ ├── rep-page01.md
│ │ └── rep-page02.md
Set in pages:
title: Mammals Overview
module: mammals
---
docs.yml:
- module: mammals
title: Section Title
docs:
- mam-page01
- mam-page02
- module: reptiles
title: Section Title
docs:
- rep-page01
- rep-page02
In my nav.html file I've tried sorting with {% for section in site.data.docs | where page.module == page.module %} or page.module == section.module with no luck. I've also tried creating nested collections with 3.7.0 and collections_dir:, also failing. Every page is available to each other in all navs:
navigation of site
How can I set the nav to only show same-module pages? I have 15 modules and with like some sort of global sorting without having to code for all sub-directories.
Thank you for your help/patience! I'm very green to this.
I've bypassed a collections directory and _config.yml restructure but adding additional variables to by assign output:
{% assign docs = site.data.docs[page.module] | map: 'docs' | join: ',' | split: ',' %}
{% for document in docs %}
{% assign dir = page.module | prepend: "/" | append: "/" %}
{% assign document_url = document | prepend: {{dir}} | append:"/" %}
{% if document_url == page.url %}
"Previous"/"Next" buttons will only link to items with the same module. A simple solution that took me forever to divine.
I have my server working perfectly with Parse-server and heroku.
One of the things I'm missing is that I want one of my cloud code functions to be called at 1:00 am each day. Is there any way I can automate this? Maybe with Parse or with another service? I just need to call that method 1 time per day.
So after some research on Github and on npm packages I have found this one.
First you install node-schedule A cron-like and not-cron-like job scheduler for Node.
You can install it through Putty in your server with this command npm install node-schedule
After you install it successfully you can open your main.js file that you have your Cloud Code and paste the following code
var schedule = require('node-schedule');
var j = schedule.scheduleJob(' * * * * *', function(){
console.log('This console log will run every 5 minutes');
});
The meaning of the stars
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
So you can have the complete code running like this if you want it to run 5 seconds (to see if its working by replacing your cloud code)
var schedule = require('node-schedule');
var j = schedule.scheduleJob('*/5 * * * *', function(){
Parse.Cloud.define("averageStars", function(request, response) {
var query = new Parse.Query("Review");
query.equalTo("movie", request.params.movie);
query.find({
success: function(results) {
var sum = 0;
for (var i = 0; i < results.length; ++i) {
sum += results[i].get("stars");
}
response.success(sum / results.length);
},
error: function() {
response.error("movie lookup failed");
}
});
});
});
Useful links:
node schedule
Try this one and I'm waiting for your results!
I'm using Laravel and trying to grab info from a foreign relationship of a member of a pivot. ERD is below. I have set up a many to many relationship so that I can grab property restrictions with $property->restriction and the other way around. What I'm struggling with is how I can add in the restriction type description when I grab this info.
$property->restriction gives me the type_id
array(
'id' => '1',
'description' => 'Fish only',
'restriction_types_id' => '1',
'pivot' => array(
'property_id' => '17',
'restriction_id' => '1'
)
)
but I want something like this with the restriction_type description instead of its id.
array(
'id' => '1',
'description' => 'Fish only',
'restriction_type' => 'Animals',
'pivot' => array(
'property_id' => '17',
'restriction_id' => '1'
)
)
My DB tables
╔═════════════╗ ╔════════════════════╗ ╔════════════════════╗
║ property ║ ║property_restriction║ ║restriction ║
╟─────────────╢ ╟────────────────────╢ ╟────────────────────╢
║id ║——————║property_id ║ ┌──║restriction_id ║
║description ║ ║restriction_id ║───┘ ║desctiption ║
╚═════════════╝ ╚════════════════════╝ ┌──║restriction_type_id ║
│ ╚════════════════════╝
│
│
╔═══════════════════╗ │
║restriction_type ║ │
╟───────────────────╢ │
║id ║────┘
║description ║
╚═══════════════════╝
I believe the way you are currently trying to access the data is through a dynamic property. It may be possible to just type out $property->restriction->restriction_type.
You can also try to eager load it in one of the ways below:
$property->with('restriction','restriction.restriction_type')->get();
or
$property
->with('restriction')
->with('restriction.restriction_type')
->get();
If neither of these ways work I would research eager loading and the dynamic properties of Laravel and see if there is something that needs to get altered in your relationships.