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]);
Related
I currently have the below array structure that I get when running Bonus::with('users')->get()
I have tried adding the array and merging similar keys but it is not working. I tried using the helper methods provided in laravel but I don't seem to be able to get it working.
[
{
"id": 1,
"users_id": 1,
"bonus_type_id": 1,
"is_paid": 0,
"amount": "100.00",
"description": "desc",
"note": null,
"created_by": 2,
"last_modified_by": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"user": {
"id": 1,
"name": "test",
"email": "test#testing.com",
"email_verified_at": null,
"status": 1,
"created_at": "2019-07-18 11:41:35",
"updated_at": "2019-07-18 11:41:35",
"deleted_at": null
}
},
{
"id": 2,
"users_id": 1,
"bonus_type_id": 2,
"is_paid": 0,
"amount": "100.00",
"description": "desc",
"note": null,
"created_by": 2,
"last_modified_by": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"user": {
"id": 1,
"name": "test",
"email": "test#testing.com",
"email_verified_at": null,
"status": 1,
"created_at": "2019-07-18 11:41:35",
"updated_at": "2019-07-18 11:41:35",
"deleted_at": null
}
},
{
"id": 3,
"users_id": 2,
"bonus_type_id": 2,
"is_paid": 1,
"amount": "100.00",
"description": "desc",
"note": null,
"created_by": 2,
"last_modified_by": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"user": {
"id": 1,
"name": "another test",
"email": "another#testing.com",
"email_verified_at": null,
"status": 1,
"created_at": "2019-07-18 11:41:35",
"updated_at": "2019-07-18 11:41:35",
"deleted_at": null
}
}
]
I want the result to be something like that
[
{
user_id: 1,
name: "test",
bonuses: [
{
bonus_type_id: 1,
amount: 100,
is_paid: 0,
},
{
bonus_type_id: 2,
amount: 100,
is_paid: 0,
}
],
},
{
user_id: 2,
name: "another",
bonuses: [
{
bonus_type_id: 2,
amount: 100,
is_paid: 1,
},
]
}
]
You need to change your eloquent relationship. Define hasMany relationship function bonuses inside User model for bonuses table and use that to get the desired result like so:
User::with('bonuses')->get();
That way it will return results from users table and populate bonuses for each user.
makes sense?
You can get specific information with select subquery, if your relations are correctly set, this should work.
User::select('user_id','name','bonus_id')
->with(array('bonuses'=> function($query){
$query->select('bonus_type_id','amount','is_paid');
}))->get();
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 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
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