Laravel Array to string conversion on multiple records - mysql

I'm trying to salve multiple records at once..
When I try to store multiple records it gives an error of array to string conversion.
This is the response I get from axios.
array:2 [
0 => array:3 [
"property_type_id" => 2
"fee_value" => "123"
"product_service_id" => 1
]
1 => array:3 [
"property_type_id" => 1
"fee_value" => "3333"
"product_service_id" => 1
]
]
$feeByPropertyType = $request->feeByPropertyType;
foreach ($feeByPropertyType as $row)
{
$data[] = [
'property_type_id' => $row['property_type_id'],
'fee_value' => $row['fee_value'],
'product_service_id' => 1
];
}
PaymentTypeProductService::insert($data);
I have tried a few other methods as well but none of them have worked so far.
Any help from the community will be greatly appreciated, on how to correct this.

You need to format your $data to be like this.
[
[
'property_type_id' => 2,
'fee_amount' => "123"
],
[
'property_type_id' => 1,
'fee_amount' => "1312"
],
]

In this case, if the data structure of the $request->feeByPropertyType data structure matches with the database tables structure, then you can simply use DB facade to insert multiple rows at once.
DB::insert($request->feeByPropertyType);

Related

REST API: how to post a manytomany relationship with fosrestbundle

In my Symfony 4 project, I have 2 entities: Question and PossibleAnswer. A question can have many possible answers and an answer can be used in many different questions.
I want to POST a question, I use FOSRestBundle. This is how I would format the body of my request:
{
"title": "my question",
"possibleAnswers": [
{
"id": 1,
"title": "my first answer"
}
]
}
At least this is how FOSRestBundle formats the answer when I create the data directly in the database and GET the question.
But I keep getting the same error:
Validation Failed, possibleAnswers This value is not valid
I use Symfony forms to validate my POST and the fieldType I use for the possibleAnswers field of Question is an EntityType with the option multiple set to true. Maybe that's where the error comes from.
So my question is: How do I format the body of my request to add a valid possibleAnswers field ? and if I am doing it right then, which form type should I use for it to work ?
Sorry for not adding any code, the context is actually quite more complex but I'm pretty sure either the JSON Format of my field or the form type is wrong.
[edit]
here are all the form types I tried for the possibleanswers field
//->add('possibleAnswers', EntityType::class, array('data' => [], 'required' => false, 'multiple' => true, 'class' => PossibleAnswer::class))
->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => EntityType::class, 'entry_options' => array('class' => PossibleAnswer::class)))
//->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => PossibleAnswerType::class))
//->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => PossibleAnswerType::class, 'entry_options' => array('class' => PossibleAnswer::class)))
here are all the JSON formats I tried to select the possibleanswers:
{
"title": "a postman created question",
"answerConditions": [
{
"id": 1,
"title": "already existing question",
}
]
}
{
"title": "a postman created question",
"answerConditions": [
{
"possibleanswer_id": 1
}
]
}
{
"title": "a postman created question",
"answerConditions": [
{
"id": 1
}
]
}
{
"title": "a postman created question",
"answerConditions": [
1
]
}
If you want to create the PossibleAnswer when posting the Question, you need to replace in your form the EntityType by a CollectionType.
I often use it in my POST request when I want to post the sub-object (here the PossibleAnswer) when I post the parent (Question) :
Question form
$builder
->add('possibleAnswers', CollectionType::class, array(
'entry_type' => PossibleAnswerType::class,
'allow_add' => true,
))
;
PossibleAnswer form :
$builder
->add('title')
;
And the json looks like :
{
"title": "my question",
"possibleAnswers": [
{
"title": "my first answer"
}
]
}
Hope it can help.
Edit :
If you want to link a PossibleAnswer to a Question while creating you Question you can use this :
Question form :
$builder
->add('possibleAnswer', EntityType::class, array(
'class' => YOUR_ENTITY::class,
'multiple' => true
));
And you json should only contains the id of the entity you want to link to. It should be like this :
{
"title": "a postman created question",
"answerConditions": [
1
]
}

CSV data saving Elasticsearch using Logstash

I want to save CSV data in Elasticsearch using Logstash to receive the following result:
"my_field": [{"col1":"AAA", "col2": "BBB"},{"col1":"CCC", "col2": "DDD"}]
So, it's important that CSV data gets saved as the array [...] in a specific document.
However, I get this result:
"path": "path/to/csv",
"#timestamp": "2017-09-22T11:28:59.143Z",
"#version": "1",
"host": "GT-HYU",
"col2": "DDD",
"message": "CCC,DDD",
"col1": "CCC"
It looks like only the last CSV row gets saved (because of overwriting). I tried to use document_id => "1" in Logstash, but it obviously provokes the overwriting. How can I save data in the array?
Also, I don't understand how to define that the data gets saved in my_field.
input {
file {
path => ["path/to/csv"]
sincedb_path => "/dev/null"
start_position => beginning
}
}
filter {
csv {
columns => ["col1","col2"]
separator => ","
}
if [col1] == "col1" {
drop {}
}
}
output {
stdout { codec => rubydebug }
elasticsearch {
action => "update"
hosts => ["127.0.0.1:9200"]
index => "my_index"
document_type => "my_type"
document_id => "1"
workers => 1
}
}

Laravel create nested json response

How to create a nested json response in Laravel 5? For example:
"response":["status":"OK",
"data":{ "user":{"name": "Shriyansh",
"email":"some#email.com",
"contact":"1234567890",
"fcmToken":"Token#123"
},
"event":{"status":"successful",
"status_code":4
}
}
]
Create an array to hold your response like this:
$data = [
"status"=> "OK",
"data"=> [
"user" => [
"name"=> "Shriyansh",
"email"=>"some#email.com",
"contact"=>"1234567890",
"fcmToken"=>"Token#123"
],
"event"=> [
"status" => "successful",
"status_code" => 4
]
]
]
Then return your data using Laravel response method like this:
return response()->json($data, 200);
So Laravel will convert your array into json format and send it back to your client.
You should try this:
$json = json_encode(array(
"status"=>"OK",
"user" => array(
"name"=> "Shriyansh",
"email"=>"some#email.com",
"contact"=>"1234567890",
"fcmToken"=>"Token#123"
),
"event" => array(
"status" => "successful",
"status_code" => 4
)
));
You can use JSON Response to return the JSON in Laravel.
Example:
Assuming you have the required data in the $data variable,
return response()->json([
'data' => $data
],200);
where, 200 is status code. status will be added as per the status code by laravel.

Logstash filter parse json file result a double fields

I am using the latest ELK (Elasticsearch 1.5.2 , Logstash 1.5.0, Kibana 4.0.2)
I have a question that
sample .json
{ "field1": "This is value1", "field2": "This is value2" }
longstash.conf
input {
stdin{ }
}
filter {
json {
source => "message"
add_field =>
{
"field1" => "%{field1}"
"field2" => "%{field2}"
}
}
}
output {
stdout { codec => rubydebug }
elasticsearch {
host => "localhost"
index => "scan"
}
}
Output:
{
"message" => "{ \"field1\": \"This is value1\", \"field2\": \"This is value2\" }",
"#version" => "1",
"#timestamp" => "2015-05-07T06:02:56.088Z",
"host" => "myhost",
"field1" => [
[0] "This is value1",
[1] "This is value1"
],
"field2" => [
[0] "This is value2",
[1] "This is value2"
]
}
My question is 1) why the field result appear double in the result? 2) If there is nested array , how is it should reference in the logstash configure?
Thanks a lot!
..Petera
I think you have misunderstood what the json filter does. When you process a field through the json filter it will look for field names and corresponding values.
In your example, you have done that with this part:
filter {
json {
source => "message"
Then you have added a field called "field1" with the content of field "field1", since the field already exists you have just added the same information to the field that was already there, it has now become an array:
add_field =>
{
"field1" => "%{field1}"
"field2" => "%{field2}"
}
}
}
If you simplify your code to the following you should be fine:
filter {
json {
source => "message"
}
}
I suspect your question about arrays becomes moot at this point, as you probably don't need the nested array, and therefore, won't need to address it, but in case you do, I believe you can do this like so:
[field1][0]
[field1][1]

Laravel 4 Response

how to return response in laravel 4 like for this example ?
"key": {
"q1": "a3",
"q2": "a2",
"q3": "a1"
}
I do simple response
$key = KeyV2::all();
return Response::json(array(
'key' => $key
), 200);
result is
"key": {
"name": "q1",
"value": "a3"
}
but I need as I have described above.
I know that when I do ALL () it returns to me in this collection does not necessarily write toArray()
You can use lists() for that:
$key = KeyV2::lists('value', 'key');
return Response::json(array(
'key' => $key
));
(And 200 is unnecessary since it is the default value)