Laravel iterate complex object - laravel

I'm having some trouble to iterate over the object (below) as it has the main User data and then the profile, jobs and photos arrays.
I'm getting the errors Invalid argument supplied for foreach()
Thanks in advance for any help here as I seem to got into a mental loop and cannot find what I'm doing wrong.
In the Controller iI call the DB:
$team = \App\User::where('users.state', 1)->with('profile','jobs','photos')->get();
$team->jsonSerialize();
What I've done to loop in the view for the variable that carries the object that gives me an error
<li>
{!! trans('global.words.partners') !!}
<ul class="dropdown">
#foreach($team as $member)
#foreach($member->jobs as $jobs)
// just display if partner | socio
#if ($jobs->slug== 'socio' || $jobs->slug == 'partner')
#foreach($member->profile as $profile)
<li>
<a href="/{!! str_slug(trans('global.words.lawyer_male'), '-') !!}/{!! $member->profile->full_name_slug !!}" title="{!! $member->profile->full_name !!}"><img class="avatar" src="{{ asset('/images/staff/thumbs/'.$member->photos[3]->filename) }}" alt="{!! $member->profile->full_name !!}"> {!! $member->profile->full_name !!}
</a>
</li>
#endforeach
#endif
#endforeach
#endforeach
</ul>
</li>
The object (reduced version):
0 => array:15 [▼
"id" => 2
"guid" => "0b6e0ad2-7daa-4c2b-907a-d095ab577851"
"name" => "John Doe"
"slug" => "john-doe"
"email" => "mp#testingsite.com"
"state" => 1
"ordering" => 0
"created_by" => 1
"updated_by" => null
"deleted_by" => null
"created_at" => "2018-02-12 19:59:47"
"updated_at" => "2018-02-12 19:59:52"
"profile" => array:23 [▼
"id" => 2
"user_id" => 2
"full_name" => "John Doe McNamara"
"full_name_slug" => "john-doe-mcnamara"
"short_name" => "John Doe"
"short_name_slug" => "john-doe-mcnamara"
"gender" => "male"
"birth_date" => "1973-05-05"
"work_start" => "2000-01-01"
"work_end" => null
"biography" => "John Doe started the firm in 1900."
"experience" => "A couple of years"
"education" => """
UCLA;\n
Berkley
"""
"affiliations" => ""
"notes" => ""
"hits" => 3
"state" => 1
"ordering" => 0
"created_by" => 1
"updated_by" => null
"deleted_by" => null
"created_at" => "2018-02-12 20:07:00"
"updated_at" => "2018-02-24 12:32:52"
]
"jobs" => array:2 [▼
0 => array:15 [▼
"id" => 1
"guid" => "45f91a68-d219-42b1-8980-8cb77d6688b4"
"type" => "Lawyer"
"slug" => "lawyer"
"department" => "Law"
"notes" => null
"language" => "en"
"state" => 1
"ordering" => 4
"created_by" => 1
"updated_by" => null
"deleted_by" => null
"created_at" => "2018-02-11 01:43:04"
"updated_at" => null
"pivot" => array:2 [▼
"user_id" => 2
"job_id" => 1
]
]
1 => array:15 [▼
"id" => 11
"guid" => "fcf9f310-eeb1-4ff4-b133-6c80d6bf3b7f"
"type" => "Partner"
"slug" => "partner"
"department" => "Law"
"notes" => null
"language" => "en"
"state" => 1
"ordering" => 1
"created_by" => 1
"updated_by" => null
"deleted_by" => null
"created_at" => "2018-02-11 01:49:38"
"updated_at" => null
"pivot" => array:2 [▼
"user_id" => 2
"job_id" => 11
]
]
]
"photos" => array:4 [▼
0 => array:14 [▼
"id" => 5
"guid" => null
"user_id" => 2
"type" => "profile"
"directory" => "staff"
"subdirectory" => "profile"
"filename" => "john-doe#1-100.jpg"
"state" => 1
"ordering" => 0
"created_by" => 1
"updated_by" => null
"deleted_by" => null
"created_at" => "2018-02-12 20:03:31"
"updated_at" => null
]
1 => array:14 [▼
"id" => 6
"guid" => null
"user_id" => 2
"type" => "bgs"
"directory" => "staff"
"subdirectory" => "bgs"
"filename" => "john-doe#1-100.jpg"
"state" => 1
"ordering" => 0
"created_by" => 1
"updated_by" => null
"deleted_by" => null
"created_at" => "2018-02-12 20:03:49"
"updated_at" => null
]
2 => array:14 [▶]
3 => array:14 [▶]
]
]
1 => array:15 [▶]
2 => array:15 [▶]
3 => array:15 [▶]
4 => array:15 [▶]
5 => array:15 [▶]
6 => array:15 [▶]
7 => array:15 [▶]
8 => array:15 [▶]
9 => array:15 [▶]
10 => array:15 [▶]
11 => array:15 [▶]
12 => array:15 [▶]
13 => array:15 [▶]
]

You are getting an error you used JsonSerializeed
Here is the code the that you can use
$team = \App\User::where('users.state', 1)->with('profile','jobs','photos')->get();
and in your view iterate over it as you already doing
<li>
{!! trans('global.words.partners') !!}
<ul class="dropdown">
#foreach($team as $member)
#foreach($member->jobs as $jobs)
// just display if partner | socio
#if ($jobs->slug== 'socio' || $jobs->slug == 'partner')
#foreach($member->profiles as $profile)
<li>
<a href="/{!! str_slug(trans('global.words.lawyer_male'), '-') !!}/{!! $member->profile->full_name_slug !!}" title="{!! $member->profile->full_name !!}"><img class="avatar" src="{{ asset('/images/staff/thumbs/'.$member->photos[3]->filename) }}" alt="{!! $member->profile->full_name !!}"> {!! $member->profile->full_name !!}
</a>
</li>
#endforeach
#endif
#endforeach
#endforeach
</ul>
</li>
Hope this helps

Related

laravel model object changed unexpectedly while doing map operation on it

This is some wierd situation in which I am there right now like I have one object
$this->remitter;
if I dump this I get this
App\Models\IpayAppBc\Remitter {#1960
#connection: "ipay_app_bc__write"
+table: "remitter"
+timestamps: false
#primaryKey: "id"
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:15 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "1234567"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
]
#original: array:15 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "9946617986"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
]
Now I did this
$remitterDetails = $this->remitter;
$remitterDetails->beneficiaries = $remitterDetails->beneficiaries->take(50)->map(function ($beneficiary) {
return [
'id' => $beneficiary->bene_id,
'name' => $beneficiary->name,
'account' => $beneficiary->account,
'ifsc' => $beneficiary->ifsc,
'bank' => $beneficiary->bank,
'verificationDt' => $beneficiary->last_success_dt,
];
});
Now the confusing part is this mapping changed my $this->remitter too
after dumping $this->remitter again i get this
App\Models\IpayAppBc\Remitter {#1960
#connection: "ipay_app_bc__write"
+table: "remitter"
#attributes: array:16 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "1234567"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
"beneficiaries" => Illuminate\Support\Collection {#1956
#items: array:2 [
0 => array:6 [
"id" => "898ddaeff5a54bf2f05a3ce00f5cd13a"
"name" => "DUMMY"
"account" => "37893810436"
"ifsc" => "SBIN0000001"
"bank" => "STATE BANK OF INDIA"
"verificationDt" => "2022-12-01 16:58:57"
]
1 => array:6 [
"id" => "c647579b5881ffc39cb2ffb1fc600634"
"name" => "MRAN DW"
"account" => "3789381043"
"ifsc" => "SBIN0000001"
"bank" => "STATE BANK OF INDIA"
"verificationDt" => null
]
]
#escapeWhenCastingToString: false
}
I am just assuming why this happened is there any referencing happening because as per my knowledge this only happens when I use referencing to any variable ?
any information on this please
Thanks

Get table multiple relations

I have the following eloquent call:
\App\Models\AwardRecommendation::with('entities', 'countries')->get();
When I ask for with 'countries' is because the 'entities' table has the country_id and not the 'awards' table.
The models I have are: AwardRecommendation, Entity, ErnySans\Laraworld\Models\Countries
What I'm trying to do is to get the country data in the query but in the return response I just get:
0 => array:27 [▼
"id" => 1
"guid" => "268f1080-67f3-48e5-844d-96b8c09c339b"
"user_id" => null
"title" => "Example Title"
"slug" => "example-title"
"intro" => null
"description" => null
"type" => "award"
"scope" => "firm"
"scope_name" => "1"
"scope_practice_area_id" => 1
"entity_id" => 1
"year" => null
"image_badge" => null
"image_one" => null
"image_two" => null
"notes" => null
"language" => "en"
"state" => 1
"ordering" => 0
"created_by" => null
"updated_by" => null
"deleted_by" => null
"created_at" => null
"updated_at" => null
"entities" => array:1 [▼
0 => array:45 [▼
"id" => 1
"guid" => "b00e2be5-e1bb-4f98-8a35-30f2f7dfceba"
"award_id" => 1
"entity_name" => "Example entity"
"slug" => "example-entity"
"url" => null
"description" => null
"title" => "Mr."
"first_name" => "John"
"last_name" => "McNamarca"
"email" => "john.mcnamara#examplecompany.com"
"department" => null
"phone" => "+1 (646) 412 123456"
"mobile_phone" => null
"home_phone" => null
"other_phone" => null
"fax" => null
"assistant" => null
"assistant_phone" => null
"do_not_call" => 0
"do_not_email" => 0
"do_not_fax" => 0
"email_opt_out" => 0
"fax_opt_out" => 0
"street" => null
"city" => null
"country_state" => null
"zip" => null
"country_id" => 240
"street_other" => null
"city_other" => null
"state_other" => null
"zip_other" => null
"country_id_other" => 240
"notes" => null
"language" => "en"
"state" => 1
"ordering" => 0
"created_by" => null
"updated_by" => null
"deleted_by" => null
"deleted_at" => null
"created_at" => null
"updated_at" => null
"pivot" => array:1 [▼
"entity_id" => 1
]
]
]
"countries" => null
How can I get this done with Laravel Eloquent?
Thanks in advance for any help
You need to define the relationship in the Entity model first:
public function country()
{
return $this->belongsTo(ErnySans\Laraworld\Models\Countries::class);
}
And make sure ErnySans\Laraworld\Models\Countries is the correct full class name for Countries model.
Then load award recommendations with related entities and with each entity's country:
AwardRecommendation::with('entities.country')->get();

How to groupby() and sortby() a Laravel collection?

This is the structure of my collection
0 => array:13 [▼
"id" => 173
"campaign_id" => "STM1234"
"product_id" => "STM1234"
"group" => "24"
"version" => "1"
"created_at" => "2017-08-04 14:02:33"
"updated_at" => "2017-08-04 14:02:33"
"groups" => array:5 [▼
"id" => 24
"campaign_id" => "STM1234"
"group_name" => "default"
"updated_at" => null
I am trying to group and order this collection.
$groups = $products->groupBy('groups.group_name')->sortBy('groups.id');
The grouping works however the ordering does not work
array:2 [▼
"group2" => array:3 [▶]
"default" => array:8 [▶]
]
Default has an id of zero so should be first what do I need to do to get the group with the id of 0 first on the list?

Display old input based on condition

I pass a Document Object to my view. If I do the following I can see the Object
{{dd($briefingDoc)}}
Now this Document has many DocumentData. If I do the following in my view
{{dd($projectIdentifiedDoc->documentData->toArray())}}
I get something like this
array:8 [▼
0 => array:7 [▼
"id" => 62
"documentId" => 13
"key" => "whatData"
"value" => "some data"
"deleted_at" => null
"created_at" => "2016-04-19 12:46:19"
"updated_at" => "2016-04-19 12:46:19"
]
1 => array:7 [▼
"id" => 63
"documentId" => 13
"key" => "whoData"
"value" => ""
"deleted_at" => null
"created_at" => "2016-04-19 12:46:19"
"updated_at" => "2016-04-19 12:46:19"
]
2 => array:7 [▼
"id" => 64
"documentId" => 13
"key" => "startDate"
"value" => "29/04/2016"
"deleted_at" => null
"created_at" => "2016-04-19 12:46:19"
"updated_at" => "2016-04-19 12:46:19"
]
3 => array:7 [▶]
4 => array:7 [▶]
5 => array:7 [▶]
6 => array:7 [▶]
7 => array:7 [▶]
]
So my view now has the data, and I need to get the appropiate data displayed in the appropiate input. At the moment I am trying something like this
{!! Form::textArea('whatData', $projectIdentifiedDoc->documentData->value, array('class' => 'form-control')) !!}
Now obviously that wont work. I somehow need to check if the key matches the input label, and if so, display the value. This code is all wrong, but hopefully
it gives you an idea of what I am after
{!! Form::textArea('whatData', if($projectIdentifiedDoc->documentData->key == 'whatData'){$projectIdentifiedDoc->documentData->value}, array('class' => 'form-control')) !!}
Would something like this be possible?
Thanks
Try where() method:
$value = $projectIdentifiedDoc->documentData->where('key', 'whatData')->first()->value;
{!! Form::textArea('whatData', $value, array('class' => 'form-control')) !!}

Laravel 5 - Blade casting collection to array and checking its value

I pass my view a Campaign Collection. If I do
{{ dd($campaign->campaignCreatives->campaignCreativesData) }}
I get something like
Collection {#348 ▼
#items: array:2 [▼
0 => CampaignCreativesData {#349 ▼
#attributes: array:7 [▼
"id" => "5"
"name" => "creativeOption"
"label" => "Other"
"value" => "sdfsdfsdfsdfsdf"
"campaignCreativesId" => "5"
"created_at" => "2016-03-01 10:24:38"
"updated_at" => "2016-03-01 10:24:38"
]
}
1 => CampaignCreativesData {#350 ▼
#attributes: array:7 [▼
"id" => "4"
"name" => "creativeOption"
"label" => "checkboxSelection"
"value" => "Animated GIF"
"campaignCreativesId" => "5"
"created_at" => "2016-03-01 10:24:38"
"updated_at" => "2016-03-01 10:24:38"
]
}
]
}
If I cast it to an array
{{ dd($campaign->campaignCreatives->campaignCreativesData->toArray()) }}
I get something like
array:2 [▼
0 => array:7 [▼
"id" => "5"
"name" => "creativeOption"
"label" => "Other"
"value" => "sdfsdfsdfsdfsdf"
"campaignCreativesId" => "5"
"created_at" => "2016-03-01 10:24:38"
"updated_at" => "2016-03-01 10:24:38"
]
1 => array:7 [▼
"id" => "4"
"name" => "creativeOption"
"label" => "checkboxSelection"
"value" => "Animated GIF"
"campaignCreativesId" => "5"
"created_at" => "2016-03-01 10:24:38"
"updated_at" => "2016-03-01 10:24:38"
]
]
Now I have several checkboxes, and if the value is in the array, I need to check the checkbox. At the moment I have something like this
{!! Form::checkbox('creativeOptions[]', 'Animated GIF', in_array('Animated GIF', $campaign->campaignCreatives->campaignCreativesData->toArray()), ['class' => 'styled', 'id' => 'checkbox1']) !!}
As you can see, Animated GIF is in the array, but the checkbox is not checked.
How can I get the checkbox checked based on what is in the array?
Thanks
You can use the method contains in Laravel collection.
{!! Form::checkbox('creativeOptions[]',
'Animated GIF',
$campaign->campaignCreatives->campaignCreativesData->contains('value', 'Animated GIF'),
['class' => 'styled', 'id' => 'checkbox1']) !!}

Resources