How to store multiple values in one column
The problem is you are passing an array to service id.
In your case the key will be services_select and value is an array in your foreach loop.
Try this
$services = $request->except('_token');
foreach ($services['services_selects'] as $id => $value) {
DB::table('services')->insert(['service_id' => $value]);
}
Hope this will help.
How to store multiple values in single field in SQL database?
You could use a second table to store the numbers, and link back with a Foreign Key:
PersonTable: PersonId, Name, etc..
The second table will hold the numbers...
NumbersTable: NumberId, PersonId(fk), Number
You could then get the numbers like this...
SELECT p.Name, n.Number from PersonTable p Left Join NumbersTable n
on p.PersonId = n.PersonId
This is a simple example. I have used a LEFT JOIN
here in case a person doesn't supply their number. Also, this is just pseudo code, so don't use Table in the name.
How to store multiple values in to objects within an array
An example using index as unique for displaying purpose.
const data = {
items: [
{
id: 119603782,
node_id: "MDEwOlJlcG9zaXRvcnkxMTk2MDM3ODI=",
name: "react-contextual",
full_name: "drcmda/react-contextual",
private: false,
owner: {
login: "drcmda",
id: 2223602,
},
},
{
id: 119603782,
node_id: "MDEwOlJlcG9zaXRvcnkxMTk2MDM3ODI=",
name: "react-contextual",
full_name: "drcmda/react-contextual",
private: false,
owner: {
login: "drcmda",
id: 2223602,
},
},
],
};
const items = data.items.map((item, idx) => ({ ...item, idx }));
// new item
const newItem = {
id: 119603782,
node_id: "MDEwOlJlcG9zaXRvcnkxMTk2MDM3ODI=",
name: "react-contextual",
full_name: "drcmda/react-contextual",
private: false,
owner: {
login: "drcmda",
id: 2223602,
},
};
function addNewItems(items, newItem) {
newItem.idx = items.length;
items.push(newItem);
return items;
}
console.log(addNewItems(items, newItem));
How to store array or multiple values in one column
You have a couple of questions here, so I'll address them separately:
I need to store a number of selected items in one field in a database
My general rule is: don't. This is something which all but requires a second table (or third) with a foreign key. Sure, it may seem easier now, but what if the use case comes along where you need to actually query for those items individually? It also means that you have more options for lazy instantiation and you have a more consistent experience across multiple frameworks/languages. Further, you are less likely to have connection timeout issues (30,000 characters is a lot).
You mentioned that you were thinking about using ENUM. Are these values fixed? Do you know them ahead of time? If so this would be my structure:
Base table (what you have now):
| id primary_key sequence
| -- other columns here.
Items table:
| id primary_key sequence
| descript VARCHAR(30) UNIQUE
Map table:
| base_id bigint
| items_id bigint
Map table would have foreign keys so base_id maps to Base table, and items_id would map to the items table.
And if you'd like an easy way to retrieve this from a DB, then create a view which does the joins. You can even create insert and update rules so that you're practically only dealing with one table.
What format should I use store the data?
If you have to do something like this, why not just use a character delineated string? It will take less processing power than a CSV, XML, or JSON, and it will be shorter.
What column type should I use store the data?
Personally, I would use TEXT
. It does not sound like you'd gain much by making this a BLOB
, and TEXT
, in my experience, is easier to read if you're using some form of IDE.
How to store multiple values into an array and then to DB?
Assuming the Invoice field is a varchar field you can build a string with all values and store that result:
var sb = new StringBuilder();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
sb.Append('<');
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
sb.Append(dataGridView1.Rows[row.Index].Cells[col.Index].Value);
sb.Append(' ');
}
sb.Append('>');
}
var toInvoiceField = sb.ToString();
How to get the all the values in multiple columns and store them in one variable
Something like this?
function percentage(){
const thisSheet = SpreadsheetApp.openByUrl(THIS).getSheetByName('WP_Data');
var values = thisSheet.getRange(3,14,thisSheet.getLastRow()-2,7).getValues(); // N3:T??
var results = [];
var i = 0;
for( i=0; i<values.length; i++ ) {
results.push([values[i][0]+values[i][2]+values[i][4]+values[i][6]]); // index 0 = column N, etc.
}
// don't know what you want to do with it but you could use setValues(results)
thisSheet.getRange(??,??,results.length,1).setValues(results);
}
Storing multiple values from one text field into an array
You can use global split
function which works on any Sequence
(including String
):
If you want it to be separated by commas only:
let array = split("x,y,z") { $0 == "," }
If you'd want to separate by either commas or spaces:
let array = split("x, y z") { contains(", ", $0) }
Related Topics
Match Only Entire Words with Like
How to Generate a Temporary Table Filled with Dates in SQL Server 2000
Use '=' or Like to Compare Strings in SQL
Can Parameterized Statement Stop All SQL Injection
Find Records Where Join Doesn't Exist
Postgres - Function to Return the Intersection of 2 Arrays
MySQL Automatically Store Record Creation Timestamp
How to Find Consecutive Rows Based on the Value of a Column
Trim Trailing Spaces with Postgresql
Is of a Type That Is Invalid for Use as a Key Column in an Index
Composite Primary Keys:Good or Bad
Use a Union or a Join - What Is Faster
Can an Inner Join Offer Better Performance Than Exists
In SQL Server How to Pivot for Multiple Columns