Nested array validation rules not working on update in laravel - arrays

I have an array like this
"fio" => "John Dou"
"city" => "1"
"birthday" => "2022-10-21"
"email" => "test#gmail.ru"
"phones" => array:2 [▼
0 => array:2 [▼
"id" => "7"
"number" => "911"
]
1 => array:2 [▼
"id" => "10"
"number" => "112"
]
]
The check for uniqueness works, but when you try to update the current record, it swears for duplication
public function rules()
{
return [
'phones.*.number' => 'required|string|max:12|unique:'.Phone::class.',number,phones.*.id';
]
}
Maybe I made a mistake somewhere?

Related

Loop through a collection of Arrays and return named key value pairs (Laravel/Eloquent)

I am working with Laravel and currently I have an eloquent Collection being returned with 3 arrays within the collection. Within each array are nested Model collections.
The structure is like so:
Illuminate\Database\Eloquent\Collection {#1905 ▼
#items: array:3 [▼
0 => app\Models\User {#1804 ▶}
1 => app\Models\User {#1805 ▶}
2 => app\Models\User {#1806 ▶}
]
}
Each user is like so when expanded
#attributes: array:3 [▼
"user_id" => 12345
"first_name" => "John"
"last_name" => "Doe"]
What I want to have returned is an array with just the user_id => 12345 for all the users.
I tried the traditional foreach but the index keep returning as 0 => 12345 instead of user_id as my key. I also tried pluck() to pull the user_id but I got the same result of a numeric key.
Is there an efficient way to achieve this using eloquent?
Pluck would not work since user_id is the value from the collection you are extracting.
Why not just do a select with only the fields you want...Instead of removing things from collection you don't need?
For example this would return an array of arrays with a key id and value being that id.
User::select('id')->get()->toArray();
[
[
"id" => 1,
],
[
"id" => 2,
],
]
But to do what you want given the data you have you could use map and return only the key-value data you need. Here is a short example:
$collection = [
[
"user_id" => 12345,
"first_name" => "John",
"last_name" => "Doe",
],
[
"user_id" => 12346,
"first_name" => "John 2",
"last_name" => "Doe",
],
[
"user_id" => 12347,
"first_name" => "John 3",
"last_name" => "Doe",
],
];
collect($collection)->map(function ($user) {
return [
"user_id" => $user["user_id"],
];
});
Keep in mind this way from an start array so in your case I am assuming you be doing like:
User::get()->map(function ($user) {
return [
'user_id' => $user->user_id,
];
});

How can I use array_replace inside foreach loop?

colums_arr:
array:4 [▼
0 => "id"
1 => "name"
2 => "productgroup"
3 => "category"
]
fields:
{#9767 ▼
+"id": array:9 [▶]
+"name": array:8 [▼
"fieldName" => "name"
]
+"productgroup": array:19 [▼
"fieldName" => "productgroup"
"mappedBy" => null
]
+"category": array:19 [▼
"fieldName" => "category"
"mappedBy" => null
]
}
What I want to do is, whenever in fields mappedBy exists for the element, I want to add name to the value. So as a result columns_arr should look like this:
array:4 [▼
0 => "id"
1 => "name"
2 => "productgroup.name"
3 => "category.name"
]
This is my approach:
foreach ($fields as $field) {
$MappedBy = isset($field['mappedBy']);
if($MappedBy != true){
$class_field = $field['fieldName'];
$key = array_search($field['fieldName'],$input);
$replace=array($key=>$class_field.".name");
$columns_arr = (array_replace($input,$replace));
}
}
The problem is, that my result is now:
array:4 [▼
0 => "id"
1 => "name"
2 => "productgroup"
3 => "category.name"
]
Why is name not added to productgroup?
I couldn't really answer your question, it's a bit unclear what you're doing and where your values are coming from etc. so I tried to rebuild your arrays and this is what I got without using array_search:
<?php
$columns = array(0 => "id", 1 => "name", 2 => "productgroup", 3 => "category");
$fields = array("id" => 9, "name" => array("fieldName" => "name"), "productgroup" => array("fieldName" => "productgroup", "mappedBy" => null), "category" => array("fieldname" => "category", "mappedBy" => null));
foreach ($fields as $key => $field) {
if($key !== "id" && $key !== "name"){
if(!isset($field['mappedBy'])){
foreach($columns as $ckey => $column){
if($column === $key){
$columns[$ckey] = $column.".name";
}
}
}
}
}
var_dump($columns);
var_dump outputs:
array(4) { [0]=> string(2) "id" [1]=> string(4) "name" [2]=> string(17) "productgroup.name" [3]=> string(13) "category.name" }
foreach ($fields as $key => $field) {
$MappedBy = isset($field['mappedBy']);
if($MappedBy != true){
$columns_arr[$key] = $field['fieldName'].".name";
}
}
You need to overwrite the correct key of your array like this.

how to display json data into laravel blade?

public function bar()
{
$lowm = new LaravelOWM();
$for = $lowm->getWeatherForecast('lahore');
$ff= json_decode(json_encode($for),true);
dd($for);
}
this is the output its I think multi-dimensional array and I just want to print only few things. Tell me the answer through for each loop.
array:3 [▼
"city" => array:6 [▼
"id" => 1172451
"name" => "Lahore"
"country" => "PK"
"population" => null
"lat" => 31.5196
"lon" => 74.3263
]
"sun" => array:2 [▼
"rise" => array:3 [▼
"date" => "2018-09-17 00:48:17.000000"
"timezone_type" => 3
"timezone" => "UTC"
]
"set" => array:3 [▼
"date" => "2018-09-17 13:05:08.000000"
"timezone_type" => 3
"timezone" => "UTC"
]
]
"lastUpdate" => array:3 [▼
"date" => "2018-09-17 23:00:29.303267"
"timezone_type" => 3
"timezone" => "UTC"
]
]
I just want to display city name, lat and lon.
Why not this?
public function bar() {
$lowm = new LaravelOWM();
$for = $lowm->getWeatherForecast('lahore');
dump('City: '. $for->city->name);
dump('Latitude: '. $for->city->lat);
dump('Longitude: '. $for->city->lon);
}
More Here

messageBag with array L5.1

I'm getting this error message bag from a RESTfull API on a Laravel client, I want to show the errors to the user, but I'm having trouble to show the errors inside the bag when they are an array.
MessageBag {#251 ▼
#messages: array:6 [▼
"gender" => array:1 [▶]
"first_name" => array:1 [▼
0 => "The first name must be at least 2 characters."
]
"last_name" => array:1 [▶]
"user_id" => array:1 [▶]
0 => array:4 [▼
"user_id" => array:1 [▶]
"address2" => array:1 [▼
0 => "The address2 must be at least 3 characters."
]
"zip" => array:1 [▶]
"phone" => array:1 [▶]
]
1 => array:4 [▼
"user_id" => array:1 [▶]
"address2" => array:1 [▼
0 => "The address2 must be at least 3 characters."
]
"zip" => array:1 [▶]
"phone" => array:1 [▶]
]
]
#format: ":message"
}
I know the normal way to show errors for example to print the gender errors I can do this one:
{!! $errors->first('gender', '<label class="control-label"><i class="glyphicon glyphicon-menu"></i> :message</label>') !!}
Or to check if 'gender' error exists:
{!! $errors->has('gender') ? 'has-error' : '' !!}
But how I do to check if the array 0 exists and show the errors?
Thank you :)
Currently on Laravel 5.1 printing MessageBags with Arrays is not posible, my solution was to instance a different MessageBag for the secondary errors instead of nest it on the main MessageBag.
Another posible solution was to nest it like:
MessageBag {#251 ▼
#messages: array:6 [▼
"gender" => array:1 [▶]
"first_name" => array:1 [▼
0 => "The first name must be at least 2 characters."
]
"last_name" => array:1 [▶]
"user_id.0.errors" => array:1[▼
0 => "True"
]
"user_id.0.error_name" => array:1[▼
0 => "The first name must be at least 2 characters"
]
"user_id.1.errors" => array:1[▼
0 => "True"
]
"user_id.1.error_name" => array:1[▼
0 => "The first name must be at least 2 characters"
]
]
#format: ":message"
}
But is not a clean way to do it, as I said, create a secondary MessageBag was more clean and easy :)

How do I access request arrays within arrays

I have the following request;
Collection {#278
#items: array:3 [
0 => array:2 [
0 => array:8 [
"id" => 631
"name" => "OIL, FILTER O.E.M."
"partno" => "235-00"
"oemnumber" => "16099 003"
"stock" => 0
"price" => "30"
"qty" => 1
"total" => 30
]
1 => array:8 [
"id" => 23
"name" => "SPEEDOMETER"
"partno" => "122-"
"oemnumber" => "25005 1013"
"stock" => 0
"price" => "276"
"qty" => 1
"total" => 276
]
]
1 => array:2 [
0 => array:2 [
"description" => "Oil change"
"hours" => "1"
]
1 => array:2 [
"description" => "Tune up"
"hours" => "2"
]
]
2 => array:15 [
"id" => 1
"custId" => 9046
"bikeId" => 5238
"trans" => "yes"
"transDetails" => "call cab"
"policies" => "Yes"
"locker" => "1"
"lockerContents" => "stuff"
"estimate" => "Yes"
"oldparts" => "Yes"
"status" => "Pending"
"created_by" => null
"created_at" => "2016-05-19 14:40:59"
"updated_by" => null
"updated_at" => "2016-06-08 09:06:58"
]
]
}
I am getting this through;
$collection = collect($request->all());
How should I go about accessing the attributes in these arrays? I have tried pluck with no joy. I suspect I could do a loop over them but with no array_expression for the arrays do I need to use the index?

Resources