messageBag with array L5.1 - arrays

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 :)

Related

Nested array validation rules not working on update in laravel

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?

Laravel - Access relations property

I need to access at the email value; it become from a relation hasOnce(),
this is dd($this->order_details)
Illuminate\Database\Eloquent\Collection {#2322 ▼ #items: array:1 [▼
0 => App\Order {#2327 ▼
#hidden: array:1 [▶]
#connection: "mysql"
#table: "orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:12 [▶]
#original: array:12 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: array:1 [▶]
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"user" => App\User {#2356 ▼
#dispatchesEvents: array:1 [▶]
#fillable: array:3 [▶]
#hidden: array:9 [▶]
#casts: array:1 [▶]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:20 [▼
"id" => 36
"email" => "email#gmail.com"
"password" => "$2y$10$f.u7MO6ZZ4GBf1uaabiF9OXerh0SR7JzUV9M5PGI1IG1xFATkeTSG"
"name" => "NAME"
"surname" => "SURNAME"
"phone" => "+39331199222"
"confirm_code" => 111111
"confirm_code_date" => "2020-06-01 18:17:42"
"confirm_tries" => 0
"city_id" => 5662
"stripe_customer_id" => "cus_HPVN1YJE0wjHtm"
"status" => "ACTIVE"
"is_admin" => 1
"price_list_id" => 1
"wallet" => 99852.6
"last_login_at" => "2020-06-05 11:49:32"
"remember_token" => null
"created_at" => "2020-06-01 18:17:42"
"updated_at" => "2020-06-08 16:33:12"
"deleted_at" => null
when i try to get it with: $this->order_details->user->email
i get back:
Property [user] does not exist on this collection instance.
I've no idea which is my error and what is wrong to get it as well,
with $this->order_details->user[0]->email
the result is the same
You are trying to access an object which is not present in the collection.
$this->order_details is a Collection
You need to access the first order, before you can access the user:
$firstOrder = $this->order_details->first();
$user = $firstOrder->user;
$email = $user->email;
// Or:
$email = $this->order_details->first()->user->email;
Of course you can also iterate over the orders in the collection, but for now I assumed you only need the first order.

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

how to iterate through data in Blade views (Laravel 5.2)

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

Laravel 5.1: Flatten Multidimensional array without removing keys

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"
]
}

Resources