I want to store data from a form into two tables on database when the radio button value on my view is "new", but the below problem was happened. But if the value is "existing", it's works fine.
what wrong with my code ?
Array to string conversion (SQL: insert into customers (company_name, address, service_id, tc_name, tc_dept, tc_phone, tc_email, bill_name, bill_dept, bill_phone, bill_email, updated_at, created_at) values (PT Bank ABC, JL Sudirman, 1, Budi, Technical, 0812345678, budi#abc.co.id, Joko, Finance, 08123456789, joko#abc.co.id, 2016-12-14 11:21:26, 2016-12-14 11:21:26))
here my store code
if($request->select_data == 'new'){
$customer = New Customer;
$customer->company_name = $request->company_name;
$customer->address = $request->address;
$customer->service_id = $request->service_id;
$customer->tc_name = $request->tc_name;
$customer->tc_dept = $request->tc_dept;
$customer->tc_phone = $request->tc_phone;
$customer->tc_email = $request->tc_email;
$customer->bill_name = $request->bill_name;
$customer->bill_dept = $request->bill_dept;
$customer->bill_phone = $request->bill_phone;
$customer->bill_email = $request->bill_email;
$customer->save();
$salesorder = New SalesOrder;
$salesorder->pid = $request->pid;
$salesorder->project_name = $request->project_name;
$salesorder->customer_id = $request->company_id;
$salesorder->total = $request->totalfee;
$salesorder->status = 'submit';
$salesorder->detail = $request->detail;
$salesorder->save();
}else{
$salesorder = New SalesOrder;
$salesorder->pid = $request->pid;
$salesorder->project_name = $request->project_name;
$salesorder->customer_id = $request->company_id;
$salesorder->total = $request->totalfee;
$salesorder->status = 'submit';
$salesorder->detail = $request->detail;
$salesorder->save();
//dd($salesorder);
}
dd($request->all()); result
array:32 [▼
"sales_order_id" => "9"
"select_data" => "new"
"company_id" => "2"
"company_name" => "PT Bank ABC"
"address" => "JL Sudirman"
"tc_name" => "Budi"
"tc_dept" => "Technical"
"tc_phone" => "0812345678"
"tc_email" => "budi#abc.co.id"
"bill_name" => "Joko"
"bill_dept" => "Finance"
"bill_phone" => "08123456789"
"bill_email" => "joko#abc.co.id"
"pid" => "PID002"
"project_name" => "Implementasi"
"order_identifier" => array:2 [▶]
"service_name" => array:2 [▶]
"service_id" => array:2 [▶]
"order_type" => array:2 [▶]
"select_plan" => array:2 [▶]
"qty" => array:2 [▶]
"unit_price" => array:2 [▶]
"total_price" => array:2 [▶]
"note" => array:2 [▶]
"emp_name" => array:1 [▶]
"emp_id" => array:1 [▶]
"position" => array:1 [▶]
"position_id" => array:1 [▶]
"mandays" => array:1 [▶]
"detail" => "Coba Coba"
"totalfee" => "3100"
"_token" => "uxmXBwJKHWIoXDSFetU4oRgTiTftYEhhdpx4CaPr"
]
radio button html code
<div class="row-fluid select_data">
<input name="select_data" id="select_data" type="radio" value="new">
<span class="lbl">New</span>
<input name="select_data" id="select_data" type="radio" value="existing">
<span class="lbl">Existing</span>
</div>
The problem is because some of $request properties are arrays. You can't store an array in integer, string etc columns. If you want to store them as arrays, you can serialize them:
$customer->service_id = json_encode(service_id);
And store in text or json columns:
$table->json('service_id');
$table->text('service_id');
Try to change id of both the radio buttons, you can not set same id for different elements, it will behave differently
Try this one
<input name="select_data" name="select_data" type="radio" value="new">
<span class="lbl">New</span>
<input name="select_data" name="select_data" type="radio" value="existing">
<span class="lbl">Existing</span>
Related
I have the following multidimensional array :
meeting[$loop->index][person]are checkboxes
meeting[$loop->index][date] are input-fields
array:9 [▼
0 => array:1 [▼
"date" => null
]
1 => array:2 [▼
"person" => "Max Example"
"date" => "10.05"
]
2 => array:1 [▼
"date" => null
]
3 => array:1 [▼
"date" => null
]
4 => array:1 [▼
"date" => null
]
5 => array:1 [▼
"date" => null
]
6 => array:1 [▼
"date" => null
]
7 => array:1 [▼
"date" => null
]
8 => array:1 [▼
"date" => null
]
]
person in this case is a checkbox, so it's only there when it's checked.
Now I want to know, how many persons are 'invited' to the meeting.
So I need to count the amount of person (check how often person exists).
When only one person is invited I want to display something else than when more people are invited.
I tried it with this:
#if ($counts = array_count_values(array_flip(array_column($ticketDaten['hefte'], 'heft'))) == 1)
[...]
but if I var_dump it I only get bool(false) in both cases (only one person or more).
You should be able to simply
echo count(array_column($meeting, 'person'));
Demo on 3v4l.org
I want to have an array with the info about each registration type associated with a registration. For example if for the registration with id "1" the user selected two registration types of type "General" and one of the type "Plus" the $regTypes array should have this content with 2 item:
'registrationTypes' => [
[
'name' => 'general',
'price' => '5',
'quantity' => '2'
],
[
'name' => 'plus',
'price' => '10',
'quantity' => '1'
]
]
The registration_types table have the name and the price. So I have this query to get some info about a registration in a conference.
$registration = Registration
::with('conference', 'Conference.registrationTypes')->where('id', 1)->first();
And then I have this code to create the array with the necessary info:
$regTypes = [];
foreach($registration->conference->registrationTypes as $key=>$registrationType){
$regTypes [
'regType' => [
'name' => $registration->conference->registrationTypes[$key]['name'],
'price' => $registration->conference->registrationTypes[$key]['price']
]
];
}
My doubt is how to also store the quantity in the $regTypes array. Because the quantity is not stored in the database.
Maybe to get the quantity is necessary to do antoher query. With this code below:
$registrationTypeDetails = Registration::with('participants:id,registration_type_id,registration_id')->find($regID);
//dd($registrationTypeDetails);
$type_counts = [];
foreach ($registrationTypeDetails->participants as $p) {
$name = $p->registration_type->name;
if (!isset($type_counts[$name])) {
$type_counts[$name] = 0;
}
$type_counts[$name]++;
}
dump($type_counts);
The $type_counts shows the quantity of each registration type associated with the registration:
array:2 [▼
"general" => 2
"plus" => 1
]
Do you know how to use this $type_counts content to store the quantity properly in the $regTypes array?
To directly answer your question (IE not change the controller code, just "use this $type_counts content to store the quantity properly in the $regTypes array"), I'm hopeful this should work for you:
$regTypes = [];
foreach($registration->conference->registrationTypes as $key=>$registrationType){
$typeName = $registration->conference->registrationTypes[$key]['name'];
$regTypes [
'regType' => [
'name' => $typeName,
'price' => $registration->conference->registrationTypes[$key]['price'],
'quantity' => $type_counts[$typeName]
]
];
}
Basically just pulling the count from the $type_counts array based on the name of the Registration Type being the key that you added in the foreach ($registrationTypeDetails->participants as $p) loop.
Depending upon what you are after, it might be easier just to loop on the registration types, rather than go through $registration->conference->registrationTypes. This way you don't have duplicates. But that assumes you don't want duplicates :)
I hope this code useful for you .If you need the number of participants in any type, you can use the following code:
$registration = Registration::with('conference','Conference.registrationTypes','Conference.registrationTypes.participants')
->where('id',$regID)->first();
$result=$registration->conference->registrationTypes->each(function ($item, $key) use($registration) {
$item['try_count']=$item->participants->count();
});
dd($result->toArray());
I added the try_count variable to the final result:
array:2 [▼
0 => array:6 [▼
"id" => 1
"name" => "general"
"price" => 0
"conference_id" => 1
"try_count" => 8
"participants" => array:8 [▼
0 => array:5 [▶]
1 => array:5 [▶]
2 => array:5 [▶]
3 => array:5 [▶]
4 => array:5 [▶]
5 => array:5 [▶]
6 => array:5 [▶]
7 => array:5 [▶]
]
]
1 => array:6 [▼
"id" => 2
"name" => "plus"
"price" => 1
"conference_id" => 1
"try_count" => 5
"participants" => array:5 [▼
0 => array:5 [▶]
1 => array:5 [▶]
2 => array:5 [▶]
3 => array:5 [▶]
4 => array:5 [▶]
]
]
]
I'm trying to store values in an array structured like this:
{"parent":
{"class":"Green","user_name":"Nitish","user_loc":"Delhi","user_id":1,"user_blockclass":null,
"child":[
{"class":"Green","user_name":null,"user_loc":null,"user_id":1,"user_blockclass":"fst",
"child":[
{"class":"Green","user_name":"pandey","user_loc":"sdgfsjd","user_id":6,"user_blockclass":"fst"},
{"class":"Green","user_name":"chaku","user_loc":"sdgjs","user_id":7,"user_blockclass":"snd"},
{"class":"Green","user_name":"iks","user_loc":"sjkdfhkjs","user_id":8,"user_blockclass":"trd"},
{"class":"Green","user_name":"yash","user_loc":"hfksjdhfk","user_id":9,"user_blockclass":"frt"},
{"class":"Green","user_name":"joshi","user_loc":"dsfh","user_id":10,"user_blockclass":"fth"}
]},
{"class":"Green","user_name":null,"user_loc":null,"user_id":1,"user_blockclass":"snd",
"child":[
{"class":"Green","user_name":"pandey","user_loc":"sdgfsjd","user_id":6,"user_blockclass":"fst"},
{"class":"Green","user_name":"chaku","user_loc":"sdgjs","user_id":7,"user_blockclass":"snd"},
{"class":"Green","user_name":"iks","user_loc":"sjkdfhkjs","user_id":8,"user_blockclass":"trd"},
{"class":"Green","user_name":"yash","user_loc":"hfksjdhfk","user_id":9,"user_blockclass":"frt"},
{"class":"Green","user_name":"joshi","user_loc":"dsfh","user_id":10,"user_blockclass":"fth"}
]},
]
}
}
I'm trying to place this in two different array and the joining them, but it is not getting the approrpiate result, I've used a variable which iterates to store the different values, if I don't use iteration it overlaps the values and only shows the last stored values. Following is my code:
$blockclass = ['fst', 'snd', 'trd', 'frt', 'fth'];
$i = 0;
$children = $user->relations()->wherePlanId($selectplan)->get();
foreach($children as $ch)
{
$j = 0;
$subuserinfo = [];
$parent_id = $ch->pivot->child;
$subuser = User::find($ch->pivot->child);
$subuserinfo['class'] = "Green";
$subuserinfo['user_name'] = $ch->name;
$subuserinfo['user_loc'] = $ch->city;
$subuserinfo['user_id'] = $ch->id;
$subuserinfo['user_blockclass'] = $blockclass[$i];
if($subuser){
$subchildren = $subuser->relations()->wherePlanId($selectplan)->get();
foreach($subchildren as $subchild)
{
$subsubuser = User::find($subchild->pivot->child);
$subsubuserinfo['class'] = "Green";
$subsubuserinfo['user_name'] = $subsubuser->name;
$subsubuserinfo['user_loc'] = $subsubuser->city;
$subsubuserinfo['user_id'] = $subsubuser->id;
$subsubuserinfo['user_blockclass'] = $blockclass[$j];
$subuserinfo['child'][$i][$j++] = $subsubuserinfo;
}
}
else
{
$subuserinfo['child'][$i][$j++] = NULL;
}
$userinfo['child'][$i++] = $subuserinfo;
}
$tree = $userinfo;
dd($tree);
Currently the data structure which I'm getting is in following format:
array:6 [▼
"class" => "Green"
"user_name" => "Nitish"
"user_loc" => "Delhi"
"user_id" => 1
"user_blockclass" => null
"child" => array:4 [▼
0 => array:6 [▼
"class" => "Green"
"user_name" => null
"user_loc" => null
"user_id" => 1
"user_blockclass" => "fst"
"child" => array:1 [▼
0 => array:5 [▼
0 => array:5 [▼
"class" => "Green"
"user_name" => "pandey"
"user_loc" => "sdgfsjd"
"user_id" => 6
"user_blockclass" => "fst"
]
1 => array:5 [▼
"class" => "Green"
"user_name" => "chaku"
"user_loc" => "sdgjs"
"user_id" => 7
"user_blockclass" => "snd"
]
2 => array:5 [▼
"class" => "Green"
"user_name" => "iks"
"user_loc" => "sjkdfhkjs"
"user_id" => 8
"user_blockclass" => "trd"
]
]
]
]
1 => array:6 [▶]
2 => array:6 [▶]
3 => array:6 [▶]
I'm not able to fetch the data in this format, also after this I want to place the data to be used as json format. Help me out.
I thinkyou should replace all $subuserinfo['child'][$i][$j++] = $subsubuserinfo; with $subuserinfo['child'][$j++] = $subsubuserinfo;
I see you are using Laravel? If you use return response()->json($tree); it will print JSON.
If you also want the parent in front of the JSON you can use this:
return response()->json(['parent' => $tree]);
I am getting json data from an api endpoint, I would like to add a key value to an array that I get. This is my function:
$magazines = Magazine::all();
foreach ($magazines as $magazine) {
$result = file_get_contents('http://customer.pages.com/?customer=' . $magazine->visio_link_prefix . '&action=latest');
$issues[] = json_decode($result, true);
}
foreach ($issues as $issue) {
Issue::create([
''
'title' => $issue['papers'][0]['title'],
'date' => $issue['papers'][0]['date'],
'foldername' => $issue['papers'][0]['foldername'],
'thumb' => $issue['papers'][0]['thumb'],
'thumbmedium' => $issue['papers'][0]['thumbmedium'],
]);
}
The array that I get from the endpoint looks like this:
array:24 [▼
0 => array:1 [▼
"papers" => array:1 [▼
0 => array:11 [▼
"title" => "News- 2014-10-22"
"date" => "2014-10-22"
"expires" => ""
"catalog" => 24
"foldername" => "News"
"folder" => 4965
"pages" => 132
"sectionstarts" => "1"
"sectioncount" => 1
"thumb" => "www.customer.pages.com/news/24/teasers/small.jpg"
"thumb_medium" => "www.customer.pages.com/news/24/teasers/medium.jpg"
]
]
]
1 => array:1 [▶]
2 => array:1 [▶]
3 => array:1 [▶]
4 => array:1 [▶]
5 => array:1 [▶]
6 => array:1 [▶]
7 => array:1 [▶]
8 => array:1 [▶]
9 => array:1 [▶]
10 => array:1 [▶]
11 => array:1 [▶]
12 => array:1 [▶]
13 => array:1 [▶]
14 => array:1 [▶]
15 => array:1 [▶]
16 => array:1 [▶]
17 => array:1 [▶]
18 => array:1 [▶]
19 => array:1 [▶]
20 => array:1 [▶]
21 => array:1 [▶]
22 => array:1 [▶]
23 => array:1 [▶]
]
So, in my foreach loop I would like to add a key value pair 'magazineId' => $magazine->id to each of the above 'papers' arrays. So that later I can use $issue['papers'][0]['magazineId'] to get the value of the $magazine->id. Not sure how to do that?
This correction to yoiur code should do the trick:
foreach ($magazines as $magazine) {
$result = file_get_contents('http://customer.pages.com/?customer=' . $magazine->visio_link_prefix . '&action=latest');
$issue = json_decode($result, true);
foreach($issue['papers'] as $paperKey => $paper) {
$issue['papers'][$paperKey]['magazineId'] = $magazine->id;
}
$issues[] = $issue;
}
foreach ($issues as $issue) {
Issue::create([
'magazineId' => $issue['papers'][0]['magazineId'],
'title' => $issue['papers'][0]['title'],
'date' => $issue['papers'][0]['date'],
'foldername' => $issue['papers'][0]['foldername'],
'thumb' => $issue['papers'][0]['thumb'],
'thumbmedium' => $issue['papers'][0]['thumbmedium'],
]);
}
And the second version where you have only one magazineId key for a single issue:
foreach ($magazines as $magazine) {
$result = file_get_contents('http://customer.pages.com/?customer=' . $magazine->visio_link_prefix . '&action=latest');
$issue = json_decode($result, true);
$issue['magazineId'] = $magazine->id;
$issues[] = $issue;
}
Could you please try code below? In first foreach I added an array to $issue.
$magazines = Magazine::all();
foreach ($magazines as $magazine) {
$result = file_get_contents('http://customer.pages.com/?customer=' . $magazine->visio_link_prefix . '&action=latest');
$issues[] = array('magazine_id' => $magazine->id, 'result' => json_decode($result, true);
}
foreach ($issues as $issue) {
Issue::create([
'magazine_id' => $issue['magazine_id'],
'title' => $issue['result']['papers'][0]['title'],
'date' => $issue['result']['papers'][0]['date'],
'foldername' => $issue['result']['papers'][0]['foldername'],
'thumb' => $issue['result']['papers'][0]['thumb'],
'thumbmedium' => $issue['result']['papers'][0]['thumbmedium'],
]);
}
i have a multidimensional array from a form that looks like this:
$items = array:8 [▼
"units" => array:4 [▼
0 => "1"
1 => "1"
2 => "1"
3 => "1"
]
"article_group" => array:4 [▼
0 => "2401"
1 => "2503"
2 => "1360"
3 => "1198"
]
"article" => array:4 [▶]
"description_en" => array:4 [▶]
"unit_price" => array:4 [▶]
"discount" => array:4 [▶]
"invoice" => array:4 [▶]
"delivery_note" => array:4 [▶]
]
The form is dynamically generated, and will not always contain 4 values, but they will always contain the same amount of values.
What i want to do is to save these records in a mysql database like this:
$bookingDetails = new bookingDetails;
$bookingDetails->units = units[0];
$bookingDetails->article_group = article_group[0];
$bookingDetails->article = article[0];
$bookingDetails->description_en = description_en[0];
$bookingDetails->unit_price = unit_price[0];
...
$bookingDetails->save();
and then the same thing with [1] and [2] and so on.
but how do i iterate through this in that way?
Regards Johan
Rather than hitting the database n number of times, in your case, count times, you can do the same in ONE HIT using bulk insert.
$your_data = array(
array('col_name_1'=>'Value 1', 'col_name_2'=>'Value 2'),
array('col_name_1'=>'Value 3', 'col_name_2'=>'Value 4')
);
your_model_name::insert($your_data);
This will improve the response time of your application.
Simply make a for or foreach loop as
for($i=0;$i<count($items['units']);$i++){
$bookingDetails->units = $items['units'][$i];
$bookingDetails->article_group = $items['article_group'][$i];
$bookingDetails->article = $items['article'][$i];
$bookingDetails->description_en = $items['description_en'][$i];
$bookingDetails->unit_price = $items['unit_price'][$i];
$bookingDetails->save();
}