This is a Laravel 5 project.
I have a standard form being submitted. I am trying to send the ID's of each account clicked from the results of the search function added within the vue component.
The details clicked are then stored in a hidden form input called 'grantedUsers'. This has the ability to store more than one value. So I've used the name grantedUsers[].
Upon submitting the form to the backend, I've DD'ed the value and it's showing all the values but in one index instead of separate indexes for each. Making it way more difficult to process data efficiently.
I'm obviously submitting the value incorrectly to the hidden input. Any assistance would be appreciated in splitting each ID into separate indexes.
Code
<input type="hidden" name="grantedUsers[]" :value="hiddenData">
//hiddenData is an empty array at initialisation.
data() {
return {
keywords: null,
results: [],
granted: [],
hiddenData: []
};
},
addUser(result) {
this.granted.push(result);
this.results.splice(this.results.indexOf(result), 1);
this.hiddenData.push(result.id);
},
removeUser(grantee) {
this.results.push(grantee);
this.granted.splice(this.granted.indexOf(grantee), 1);
this.hiddenData.splice(this.hiddenData.indexOf(grantee.id), 1);
}
//The backend is outputting this on the DD
array:1 [▼
0 => "1,2"
]
I'm trying to make it out
array:2 [▼
0 => "1"
1 => "2"
]
Adding [] to the name of an input field is only useful when you have multiple input fields with this name. It does not convert the value of a single field to an array.
So you can just remove the [] from the name and do a simple explode on the string to make it an array.
dd(explode(',', $request->grantedUsers));
Related
I have laravel livewire collection as below.
[
{
"date":"2021.09.01-0:00",
"open":110.177,
"close":110.175,
"low":110.172,
"high":110.18
},
{
"date":"2021.09.01-0:01",
"open":110.175,
"close":110.171,
"low":110.169,
"high":110.175
},
{
"date":"2021.09.01-0:02",
"open":110.171,
"close":110.173,
"low":110.17,
"high":110.176
}
]
I would like to convert them into form of square bracket collection without key name as below .
$data = [
['2021.09.01-0:00',110.177,110.175,110.172,110.18],
['2021.09.01-0:01',110.175,110.171,110.169,110.175],
['2021.09.01-0:02',110.171,110.173,110.17,110.176]
];
Any advice or guidance on this would be greatly appreciated, Thanks.
You can use collection map method:
The map method iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it
https://laravel.com/docs/8.x/collections#method-map
$expectData = $collection->map(fn($item) => [$item-> date, $item->open,...])
I am not sure what the original data is but you can map over the collection and pull the values; if Eloquent Models:
$data->map(fn ($v) => array_values($v->toArray()))
Would depend upon how you got that original Collection for how it would end up in this scenario.
If you need to restrict what attributes and the order of them returned you can use only on the model:
fn ($v) => array_values($v->only(['date', ...]))
Using laravel validation, I would like to ensure that a field is unique, but in an array context. (I have seen this and this but they don't address the array context.)
Let's suppose I have this html:
<input name="sites[1][id]"><input name="sites[1][site_mrn]">
<input name="sites[2][id]"><input name="sites[2][site_mrn]">
<input name="sites[3][id]"><input name="sites[3][site_mrn]">
In my validation rule, I want to ensure that each site's id is valid, and that the site_mrn is not blank, so I have:
public function rules()
{
return [
'sites.*.site_mrn' => 'required|min:1',
'sites.*.id' => 'exists:sites,id'
];
}
So that part works. My problem is that I want to ensure that each pair of site site_id and site_mrn are unique in the mpi_sites table, but I don't know how to access each id/site_mrn pair in the input. I want to do something like this:
'sites.*' => Rule::unique('mpi_sites')->where(function ($q) {
$q->where('site_id', $xxxxx)->where('site_mrn', $yyyyy);
})
I have an application with laravel and mongodb. With elequent iam trying to append a new value to an array stored in a field.
So this is an example of a document. Iam trying to append a new value to the projects field.
{
"_id":{
"$oid":"5f80513714450000a6007c90"
},
"organization_id":"5f80304214450000a6007c81",
"user_id":"5f80513714450000a6007c8f",
"status":true,
"role_id":"5c148783fe412ba8333074ec",
"company_id":"5f80511f14450000a6007c8e",
"updated_at":{
"$date":"2020-11-09T18:42:03.000Z"
},
"created_at":{
"$date":"2020-10-09T12:01:59.000Z"
},
"projects":[
"5fa33513416100008d0070ba",
"5f80429814450000a6007c85",
"5f80436714450000a6007c86"
]
}
in my model i have created this method
public function push_user_projects($data, $where_data) {
return UserAccountsModel::where($where_data)->put($data);
}
The where_data works good. The method finds the correct documents to update.
But what doesent work is the pushing part. The following is what i put into data parameter
$data = array(
'projects' => array(
$projectData["_id"]
)
);
And when i se the result a complete new array has updated the projects field with the new value. But i want to append the value to the existing array. Can someopne help me?
To push another element into your data variable under 'projects'
$data['projects'][] = $projectData["_id"];
I'm using Strapi GraphQL as a backend for my Nuxt app, and am trying to perform a query where I find images using an array of IDs. Right now, supplying an empty array returns all images when instead I need the query to return an empty array of Images.
images.gql:
query($ids:[ID]) {
images(where:{id:$ids}) {
id
name
slug
src {
url
formats
}
}
}
query variables:
{
"ids": [] <-------- returns all images; need to return empty array of Images
}
{
"ids": [6] <--------- returns image with ID of 6 correctly
}
Looking at the Strapi documentation I've also tried other filters such as [field]_in but that is still returning all Images instead of an empty array.
Does anyone have a suggestion? Thanks!
When using a FieldArray the data are parsed and stored in Redux as an array of objects. In my case:
[
{
email: "something#domain.ext"
},
{
email: "another#domain.ext"
}
]
I would like to use the parse and format property to store in redux an array of strings instead of objects like so:
[
'something#domain.ext',
'another#domain.ext'
]
Is this possible?
Found out i just needed to set onClick={() => fields.push()} on the Add email button. That would not create an object but just an array of plain strings.