I am in php/laravel im using foreach to access the values like:
CODE:
#foreach($users as $user)
{{$user}}
#endforeach
Result:
{
"id": 97,
"user_id": 68,
"reporting": "25",
"created_at": "2017-11-28 09:18:57",
"updated_at": "2017-11-28 09:18:57",
"reporting_users": [
{
"id": 2,
"attendanceId": 1,
"name": 68,
"attendanceDate": "2017-12-04 00:00:00",
"clockIn": "2017-12-04 05:10:00",
"clockOut": "2017-12-04 05:10:00",
"workTime": "2017-12-04 05:10:00",
"reason": "test",
"status": 1,
"created_at": null,
"updated_at": null
}
],
"find_user": {
"id": 68,
"attendances_id": 1,
"name": "Ahsan Khyraj",
"email": "ahsan22#gmail.com",
"verificationToken": "nhpyzVQr8YjFJbHvokrzjwee2",
"status": 1,
"userType": 1,
"department": 1,
"created_at": "2017-11-28 09:18:57",
"updated_at": "2017-11-28 09:18:57"
}
}
Now how can I access the value attendanceId and email ?
I am able to access the value of reporting
{{$user->reporting}}
but how can I access the other values which is inside reporting_users and find_user
Let's break it down.
reporting_users is an array (Note the [] square bracket). So you got to loop it.
find_user is an object (Note the {}), so you just access it via the ->.
#foreach($users as $user)
{{ $user->find_user->name }}
#foreach($user->reporting_users as $reporting_user)
{{ $reporting_user->name }}
#endforeach
#endforeach
Related
I have some data like below and I am trying to get an object inside the object in Laravel blade file usually it's supposed to work but here in the blade file in Laravel not working can you help me?
{
"id": 2,
"branch_id": 1,
"counselor_id": 3,
"universities": [
{
"id": 1,
"lead_id": 2,
"institute": 1,
"course": {
"id": 3,
"institute_id": 1,
"course_category_id": 4,
"course_level_id": 1,
"course_name": "BSc (Hons) Counselling",
"course_duration": "3",
"course_intake": "January, September",
"active": 1,
"created_at": "2021-06-10T06:40:45.000000Z",
"updated_at": "2021-06-10T06:40:45.000000Z",
"institute": {
"id": 1,
"represent_country_id": 1,
"institute_name": "University Name Here",
"campus_name": "London",
"active": 1,
"created_at": "2021-06-10T06:41:52.000000Z",
"updated_at": "2021-06-10T06:41:52.000000Z"
}
},
"created_at": null,
"updated_at": null
},
{
"id": 2,
"lead_id": 2,
"institute": 2,
"course": {
"id": 13,
"institute_id": 2,
"course_category_id": 10,
"course_level_id": 1,
"course_name": "BSc (Hons) Business and Events Management",
"course_duration": "3",
"course_intake": "January, May & September",
"active": 1,
"created_at": "2021-06-10T06:40:45.000000Z",
"updated_at": "2021-06-10T06:40:45.000000Z",
"institute": {
"id": 2,
"represent_country_id": 1,
"institute_name": "University Name Here",
"campus_name": "London",
"active": 1,
"created_at": "2021-06-10T06:41:52.000000Z",
"updated_at": "2021-06-10T06:41:52.000000Z"
}
},
"created_at": null,
"updated_at": null
}
]
}
blade.php
<div class="course-interest">
<ul>
#foreach ($applicant_infos['universities'] as $university)
<li>{{ $university['course']['course_name'] }}</li>
#endforeach
</ul>
</div>
it's not working. can anyone help me to how to access object inside object in laravel blade ?
First check if $university->course is not empty:
{{ $university->course ? $university->course->course_name : 'Course Not Found' }}
You can access like below if you are passing model object
#foreach ($applicant_infos->universities as $university)
<li>{{ $university->course->course_name }}</li>
#endforeach
Updated.
you have to pass data to markdown like below
return $this->markdown('emails.application_email',['app_body'=>$this->app_body,'applicant_infos'=>$this->applicant_infos]);
I need to be access each level. I am using laravel blade and php. I have been trying a few different way with no success, here is an example of what I tried:
#foreach($exercisePlan as $al)
#if(isset($al->exercises))
<li>{{$al->exercise->description}}</li>
#endif
#endforeach
Here is my Json, I would be very grateful if someone could help me out here. Thanks in advance!
{
"id": 1,
"userID": 1,
"exercisePlanID": 1,
"updated_at": null,
"created_at": null,
"exercises": [
{
"id": 1,
"planID": 1,
"trainerID": "1",
"day": "Monday",
"exercise": [
{
"id": 1,
"name": "Concentration Curls",
"bodyPart": "Arm",
"muscle": "Bicep",
"description": "While sitting on a bench with your feet firmly on the floor, place the back of your left upper arm on the inside of your thigh. Keep your arm on your thigh throughout. Put your right hand on the right knee for stability. Do your curls on the left side, then repeat on the right side. ",
"video": "https:\/\/www.youtube.com\/watch?v=ebqgIOiYGEY",
"created_at": null,
"updated_at": null
}
],
"reps": "10",
"sets": "5",
"priority": "1",
"weight": "10kg",
"created_at": null,
"updated_at": null
},
{
"id": 2,
"planID": 1,
"trainerID": "2",
"day": "Monday",
"exercise": [
{
"id": 2,
"name": "Preacher Curls",
"bodyPart": "Arm",
"muscle": "Bicep",
"description": "Using a regular preacher bench, grab an EZ Curl bar with both hands using an underhand grip (palms facing upwards). Slowly curl the bar up to the top and bring it a few inches from your chin. Return the weight back down with a slow and controlled tempo to the starting position, allowing some resistance (negative) on the way back down. Repeat the movement for the desired number of repetitions.",
"video": "https:\/\/www.youtube.com\/watch?v=fIWP-FRFNU0",
"created_at": null,
"updated_at": null
}
],
"reps": "5",
"sets": "5",
"priority": "2",
"weight": "15kg",
"created_at": null,
"updated_at": null
}
]
}
Edit now getting error:Undefined variable: exercise:
#foreach($exercisePlan as $al)
#if(isset($al->exercises))
#foreach($al->$exercises as $exercise)
<li>{{$exercise->description}}</li>
#endforeach
#endif
#endforeach
Got it:
#foreach($exercisePlan as $plan)
#foreach($plan->exercises as $exercises)
#foreach($exercises->exercise as $exercise)
{{$exercise->name}}
#endforeach
#endforeach
#endforeach
I got a some trouble in my Laravel application, in Search function, when I do search the result is
{
"data": [
{
"id": 2,
"user_id": 8,
"identity_number": "213918273",
"name": "Pekerja_2",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "4232343432",
"image": null,
"created_at": "2018-01-11 10:59:54",
"updated_at": "2018-01-11 10:59:54",
"partner_id": null,
"skill": [
{
"id": 3,
"worker_id": 2,
"skill_id": 6,
"sub_skill_id": 18,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"price": null,
"unit": null
}
]
},
{
"id": 3,
"user_id": 16,
"identity_number": "213918273",
"name": "Pekerja_3",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "2345234234",
"image": null,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"partner_id": null,
"skill": []
}
]
}
as you can see that "Skill" with id number 3 is empty, I want all in id number 3 is empty also.
My controller is :
$worker = Worker::with(['skill' => function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
}])->whereHas('skill');
And I want something like this :
{
"data": [
{
"id": 2,
"user_id": 8,
"identity_number": "213918273",
"name": "Pekerja_2",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "2534234234",
"image": null,
"created_at": "2018-01-11 10:59:54",
"updated_at": "2018-01-11 10:59:54",
"partner_id": null,
"skill": [
{
"id": 3,
"worker_id": 2,
"skill_id": 6,
"sub_skill_id": 18,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"price": null,
"unit": null
}
]
},
]
}
The point is, if "skill" variable is empty then data not show and vice versa.
Thankyou any help will appreciate, sorry for bad english
You can add a callback to your whereHas function with the same filter as the with function
$worker = Worker::with(['skill' => function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
}])
->whereHas('skill', function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
})
->get();
You can try this $worker = Worker:has('skill')->get();
Only worker that have at least one skill are contained in the collection
I have a query that runs group by and order by with eager loading and it seems group by works fine but order by doesn't work.
Message::whereIn('conversation_id',$msgs)
->where('deleted', '!=', $userId)
->with(array('last_sender' =>function($query) use ($userId){
$query->where('id','!=', $userId);
$query->select('id', 'userName', 'profilePic','firstName', 'lastName');
}))
->with(array('last_reciever' =>function($query) use ($userId){
$query->where('id','!=', $userId);
$query->select('id', 'userName', 'profilePic','firstName', 'lastName');
}))
->orderBy('id', 'desc')
->orderBy('conversation_id', 'desc')
->groupBy('id')
->groupBy('conversation_id')
->get();
It returns data like this
{
"data": [
{
"id": 133,
"conversation_id": 13,
"last_sender": null,
"last_reciever": {
"id": 55,
"userName": "buyer",
"profilePic": "pRBDBFJW55baSnF560Ajid8jTgPo5kmg4i5LMhPG.jpeg",
"firstName": "Matti",
"lastName": "Rames"
},
"msg": "second message 2 to user second",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:43:14",
"updated_at": "2017-10-17 15:43:14"
},
{
"id": 132,
"conversation_id": 11,
"last_sender": null,
"last_reciever": {
"id": 54,
"userName": "Sadek",
"profilePic": "Nir7zgorNT2dmwcXJdhNK3ZmPAltmkEnj0SXDCDC.png",
"firstName": "Sadek",
"lastName": "Hossain"
},
"msg": "second message to first user",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:38:16",
"updated_at": "2017-10-17 15:38:16"
},
{
"id": 131,
"conversation_id": 13,
"last_sender": null,
"last_reciever": {
"id": 55,
"userName": "buyer",
"profilePic": "pRBDBFJW55baSnF560Ajid8jTgPo5kmg4i5LMhPG.jpeg",
"firstName": "Matti",
"lastName": "Rames"
},
"msg": "second message to second user",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:37:49",
"updated_at": "2017-10-17 15:37:49"
},
{
"id": 130,
"conversation_id": 11,
"last_sender": null,
"last_reciever": {
"id": 54,
"userName": "Sadek",
"profilePic": "Nir7zgorNT2dmwcXJdhNK3ZmPAltmkEnj0SXDCDC.png",
"firstName": "Sadek",
"lastName": "Hossain"
},
"msg": "Hi how are you",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:36:49",
"updated_at": "2017-10-17 15:36:49"
}
]
}
But I only want to return the first two latest results. Only id 133 and 132
I know I added ->groupBy('id') which is useless here and returning the all records. I added it just to show what my data are. So if I remove ->groupBy('id') then it only returns id 131 and 130
Please help. Thank you.
Ok I solve it adding one extra line :)
I am posting for other's helps.
->orderBy('created_at', 'desc')
->get()
->unique('conversation_id');
the data i am testing on is like this in feed variable
{
"id": 32,
"user_id": 1,
"target_id": 3,
"feedable_id": 7,
"feedable_type": "review",
"created_at": "2016-05-23 14:18:22",
"updated_at": "2016-05-23 14:18:22",
"feedable": {
"id": 7,
"user_id": 1,
"slug_id": 3,
"review": "y this product is so quite?",
"stars": 9,
"created_at": "2016-05-23 14:18:22",
"updated_at": "2016-05-23 14:18:22",
"slug": {
"id": 3,
"value": "Quite",
"views": 5,
"user_id": 1,
"category_id": 1,
"created_at": "2016-05-23 14:18:03",
"updated_at": "2016-05-24 12:47:29"
}
}
}
but i am not be able to access the value:quite in my view
{{ $feed->feedable->slug->value }}
Laravel throws
Trying to get property of non-object (View: ...
This is a json variable so you need to decode it first then access the value. Using json_decode you can decode the variable and get the desire value.
Check online.
$json = '{
"id": 32,
"user_id": 1,
"target_id": 3,
"feedable_id": 7,
"feedable_type": "review",
"created_at": "2016-05-23 14:18:22",
"updated_at": "2016-05-23 14:18:22",
"feedable": {
"id": 7,
"user_id": 1,
"slug_id": 3,
"review": "y this product is so quite?",
"stars": 9,
"created_at": "2016-05-23 14:18:22",
"updated_at": "2016-05-23 14:18:22",
"slug": {
"id": 3,
"value": "Quite",
"views": 5,
"user_id": 1,
"category_id": 1,
"created_at": "2016-05-23 14:18:03",
"updated_at": "2016-05-24 12:47:29"
}
}
}';
$feed = json_decode ($json);
echo $feed->feedable->slug->value; //Quite