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
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?
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,
];
});
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 :)
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"
]
}