I'm trying to find a way to flatten a multidimensional array that doesn't remove the ID that is saved as a key.
I used this in my controller:
$dataslots = Dataslot::all()->map(function($dataslot) { return [$dataslot->id => $dataslot->maand->format('d-m-Y')]; });
The output of this is a multidimensional array that looks like:
Collection {#242 ▼
#items: array:4 [▼
0 => array:1 [▼
1 => "01-12-2015"
]
1 => array:1 [▼
2 => "01-01-2016"
]
2 => array:1 [▼
3 => "01-02-2016"
]
3 => array:1 [▼
4 => "01-03-2016"
]
]
}
I tried to use the Laravel Helper Function 'Array Flatten'. This method gives me the following result:
array:4 [▼
0 => "01-12-2015"
1 => "01-01-2016"
2 => "01-02-2016"
3 => "01-03-2016"
]
As you can see, the ID's which served as keys for my dates have been removed.
The result that I am looking for:
Collection {#243 ▼
#items: array:4 [▼
1 => "01-12-2015"
2 => "01-01-2016"
3 => "01-02-2016"
4 => "01-03-2016"
]
}
Related
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?
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.
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
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 :)
Within Blade, {{ dd(get_defined_vars()['__data']) }} gives me this output:
array:6 [▼
"__env" => Factory {#152 ▶}
"app" => Application {#2 ▶}
"errors" => ViewErrorBag {#145 ▶}
0 => array:1 [▼
"question" => "question 3"
]
1 => array:1 [▼
"question" => "question 2"
]
2 => array:1 [▼
"question" => "question 1"
]
]
My controller builds this data like so:
foreach ($questions as $question) {
$answer = [
'question' => $question->question,
];
$answers[] = $answer;
}
return view('results')->with($answers);
How do I iterate over this in Blade to display the 3 questions?
This should do the job
From laravel documentation
When passing information in this manner, $data should be an array with
key/value pairs
In Controller
return view('results')->with('answers', $answers);
In blade
#foreach($answers as $answer)
{{ $answer['question'] }}
#endforeach
Have a look at
https://laravel.com/docs/5.2/views#passing-data-to-views