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?
my api receive single array of object from client side like following:
[
{
"datetime": "2022-07-19 12:04:03",
"receiptNumber" : "4010",
"isRefund": 0,
"amount" : 1025
},
]
and whem try to validate request at api side checked success and process to database success my issue happen when try to sent multi object inside array like following :
[
{
"datetime": "2022-07-19 12:04:03",
"receiptNumber" : "4010",
"isRefund": 0,
"amount" : 1025
},
{
"datetime": "2022-07-19 12:04:03",
"receiptNumber" : "4009",
"isRefund": 0,
"amount" : 1025
}
]
when i am try to validate first object inside array using foreach like following :
$array = $request->all();
foreach($array as $sdsa){
$this->validate($sdsa, [
'datetime' => 'required|date_format:Y-m-d H:i:s',
'amount' => 'required|numeric',
'isRefund' => 'required|integer|between:0,1',
'receiptNumber' => 'required|string',
]);
}
in this case api return error :
"message": "Argument 1 passed to App\\Http\\Controllers\\Controller::validate() must be an instance of Illuminate\\Http\\Request, array given,
so that i am asking how can convert this format:
array:4 [
"datetime" => "2022-07-19 12:04:03"
"receiptNumber" => "4010"
"isRefund" => 0
"amount" => 1025
]
to
{
array:4 [
"datetime" => "2022-07-19 12:04:03"
"receiptNumber" => "4010"
"isRefund" => 0
"amount" => 1025
]
}
You can use 'json_encode()'.
Example;
$myArray = array("datetime" => "2022-07-19 12:04:03", "receiptNumber" => "4010", "isRefund" => 0, "amount" => 1025);
Output;
[
"datetime" => "2022-07-19 12:04:03",
"receiptNumber" => "4010",
"isRefund" => 0,
"amount" => 1025,
]
Than if you use json_encode like this;
$myArray = json_encode($myArray);
Output;
{
[
"datetime" => "2022-07-19 12:04:03",
"receiptNumber" => "4010",
"isRefund" => 0,
"amount" => 1025,
]
}
You need to use
Validator::make($sdsa, [
'datetime' => 'required|date_format:Y-m-d H:i:s',
'amount' => 'required|numeric',
'isRefund' => 'required|integer|between:0,1',
'receiptNumber' => 'required|string',
])->validate()
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');
}
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 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?