Retrive all data which is match from foreign key in laravel - arrays

I am having trouble fetching data from another table. It works but I only get one row for one of the relationships and I want to get all related rows with the same foreign key.
Here is my code:
controller
$reply_message = FamilyMessage::with('communitymessages')
->where('resident_id', $request->resident_id)
->where('id', $request->id)
->latest('id')
->get()->toArray();
and I got this answer:
array:1 [▼
0 => array:9 [▼
"id" => 176
"community_id" => 25
"resident_id" => 4515
"name" => "romil"
"message" => "test 1"
"status" => 1
"created_at" => "2020-01-20 11:27:39"
"updated_at" => "2020-01-21 05:28:51"
"communitymessages" => array:10 [▼
"id" => 21
"user_id" => 3
"resident_id" => 4515
"community_id" => 25
"family_message_id" => 176
"name" => " romil "
"reply_msg" => "reply test 1"
"status" => 0
"created_at" => "2020-01-20 11:28:15"
"updated_at" => "2020-01-20 11:28:15"
]
]
]
see communitymessages only get 1 row, but I want all rows where store same forign key.
I want answer like this
array:1 [▼
0 => array:9 [▼
"id" => 176
"community_id" => 25
"resident_id" => 4515
"name" => "romil"
"message" => "test 1"
"status" => 1
"created_at" => "2020-01-20 11:27:39"
"updated_at" => "2020-01-21 05:28:51"
"communitymessages" => array:10 [▼
"id" => 21
"user_id" => 3
"resident_id" => 4515
"community_id" => 25
"family_message_id" => 176
"name" => " romil "
"reply_msg" => "reply test 1"
"status" => 0
"created_at" => "2020-01-20 11:28:15"
"updated_at" => "2020-01-20 11:28:15"
],
"communitymessages" => array:10 [▼
"id" => 22
"user_id" => 3
"resident_id" => 4515
"community_id" => 25
"family_message_id" => 176
"name" => " romil "
"reply_msg" => "reply test 2"
"status" => 0
"created_at" => "2020-01-20 11:28:15"
"updated_at" => "2020-01-20 11:28:15"
],
"communitymessages" => array:10 [▼
"id" => 23
"user_id" => 3
"resident_id" => 4515
"community_id" => 25
"family_message_id" => 176
"name" => " romil "
"reply_msg" => "reply test 3"
"status" => 0
"created_at" => "2020-01-20 11:28:15"
"updated_at" => "2020-01-20 11:28:15"
]
]
]
These are the relationships:
public function communitymessages()
{
return $this->hasOne('App\CommunityMessage', 'family_message_id');
}
public function familymessages()
{
return $this->belongsTo('App\FamilyMessage','family_message_id');
}

The reason you're only getting one row is because you're telling Eloquent that it should only have one row (hasOne).
To retrieve all the related rows you want to update your relationship from hasOne to a hasMany:
public function communitymessages()
{
return $this->hasMany('App\CommunityMessage', 'family_message_id');
}

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?

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