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

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']) !!}

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

How to use whereIn() when column names don't match?

I am trying to use whereIn() to find records who have 'prov' that can be found in this array;
array:3 [▼
0 => array:6 [▼
"id" => 1
"contest_id" => 1
"contest_zone_id" => 1
"province" => "ON"
"created_at" => "2018-06-20 13:37:45"
"updated_at" => "2018-06-20 13:37:45"
]
1 => array:6 [▼
"id" => 2
"contest_id" => 1
"contest_zone_id" => 1
"province" => "SK"
"created_at" => "2018-06-20 13:37:45"
"updated_at" => "2018-06-20 13:37:45"
]
2 => array:6 [▼
"id" => 3
"contest_id" => 1
"contest_zone_id" => 1
"province" => "NB"
"created_at" => "2018-06-20 13:37:45"
"updated_at" => "2018-06-20 13:37:45"
]
]
So I can do this;
->whereIn('prov', $provinces)
However, in my 2 tables 'prov' and 'province' aren't the same so it's not working. Is there a way to explicitly declare which columns I want to compare?
Thanks!
UPDATE WITH FIX
Array needs to be pared down to just the field you are looking to match.

Laravel iterate complex object

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

How to check if multidimensional array is empty or not in laravel

I'm trying to check if there is an empty array in the nested arrays.
This is what I get from my form.
array:15 [▼
"_token" => "h4aR4xJlWhZveRKbAgHzgzHWSKSqyhVKb7OHAgWH"
"name" => "Test office"
"is_department" => "0"
"hours" => "1-3"
"description" => "Description"
"content" => "<p>Content</p>"
"street" => "123 Street"
"city" => "Foomania"
"state" => "Sweet state"
"postal" => "98234"
"phone" => "5748293212"
"fax" => "2123131233"
"email" => "test#domain.tld"
"additional-page" => ""
"office_fees" => array:4 [▼
0 => array:2 [▼
"description" => ""
"fee" => ""
]
1 => array:2 [▼
"description" => ""
"fee" => ""
]
2 => array:2 [▼
"description" => ""
"fee" => ""
]
3 => array:2 [▼
"description" => ""
"fee" => ""
]
]
]
How can I check if there is empty array in office_fees ?
Just to be clear, office_fees will always return at least one array. What I'm trying to is to be able to determine whether the office_fees need to be saved into another model.
Not sure what You're looking for but:
empty($data['office_fees'])
checks if an array isset and not empty. If You want to check an empty array try this:
if (is_array($data['office_fees']) && !empty($data['office_fees']))
In laravel 5.2 you can validate arrays
$validator = Validator::make($request->all(), [
'office_fees.*.description' => 'required',
]);
Source: Laravel documentation

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')) !!}

Resources