Display old input based on condition - laravel

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

Related

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 Eloquent eager Loading and Retrieving Values From the Array

Using Laravel's Eloquent and eager loading how would one retrieve the values in the built_load_cost array and th built_load_revenue array?
```
array:11 [▼
"id" => 5
"created_at" => "2018-03-24 00:19:52"
"updated_at" => "2018-03-24 00:20:14"
"load_number" => 1
"dispatcher_id" => 3
"carrier_id" => 826097731
"customer_id" => 1
"ip" => "127.0.0.1"
"deleted_at" => null
"built_load_cost" => array:6 [▼
"id" => 4
"created_at" => "2018-03-26 19:59:34"
"updated_at" => "2018-03-26 19:59:34"
"line_haul" => "100.00"
"extra_pickups" => "0.00"
"built_load_id" => 5
]
"built_load_revenue" => array:6 [▼
"id" => 4
"created_at" => "2018-03-26 19:59:43"
"updated_at" => "2018-03-26 19:59:43"
"line_haul" => "90.00"
"extra_pickups" => "0.00"
"built_load_id" => 5
]
]
```
When accessing value through eloquent model, use camelCase, with first lower case letter:
$load->builtLoadCost->line_haul
$load['builtLoadCost']['line_haul']
If the eloquent model is converted to array, use snake_case to access relations:
$load->toArray()['built_load_revenue']['line_haul']
See also: Laravel “with” changes variable case to snake case

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 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?

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