i've an array like below for $response
. i wanted to fetch the user
field alone & files
field lying inside the user
from it . i tried like $response['user']
& $response['user']['files']
, it shows the error undefined index
. How can i achieve this?
[
{
"id": 14,
"userId": 1,
"courseDisplayName": "yuu",
"aboutCourse": "kljkl",
"user": {
"id": 1,
"name": "admin",
"profilePicId": 93,
"aboutUser": null,
"files": {
"id": 93,
"FilePath": "contentq/contentqFiles/XwyC6JHK5T/Account.png"
}
},
"files": null
}
]
What am trying to do here is that ,the input couresId
is array like [14,15]
, So for each item in the array ,i wanted to fetch data from Course
, user
& files
tables , for which the id
of Course
table has the value taken in input, userId
of course
table has the value of id
in user
table & fileId
of course
table has the value of id
in files
table. The realtions user
& files
are defined in the Course
model.
$response=null;
foreach($request->courseId as $key => $row){
$response[] = Course::with([
'user' => function($q)
{
$q->with('files:id,FilePath')->select('id', 'name' , 'profilePicId','aboutUser');
},'files:id,FilePath'])
->where('id',$row)
->get()
->makeHidden(['subjectId','skillId','courseUniqueName','active','created_at','updated_at','deleted_at','publish','course_patterns_id','paid_status','fileId','updatedBy']);
}
**EDIT**
//$res=[];
foreach($response as $row){
$res['user'] = $row->user; // error- Property [user] does not exist on this collection instance
$res['user'] = $row->first()->user;// works , but it will show only 1 set of data. i wanted all sets of data.
}
$response is an array or collection of array. you can't access $response['user'] without looping it. Either call $response->first() or make a foreach to loop through $response
Way One:
$user = $response->first()->user;
$files = $response->first()->files;
Way Two:
foreach($response as $row){
$user = $row->user;
$files = $row->files;
// use dd() these to see what you get
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.