I want to view the loop of array in blade view, in my view i want print the array items.
I tried with this but it doesn't work
<div class="time-picker-container">
<div class="time-picker">
<ul>
#foreach($slots as $key => $slot)
<li>
<label class="time-picker-toggle-wrapper">
<input type="radio" value="" name="time-picker" />
<span class="time-name">{{ $slot->value }}</span>
</label>
</li>
#endforeach
</ul>
</div>
</div>
the array:
array:55 [▼
0 => array:2 [▼
"value" => "08:15"
"time-name" => "08:15 AM"
]
1 => array:2 [▼
"value" => "08:30"
"time-name" => "08:30 AM"
]
2 => array:2 [▼
"value" => "08:45"
"time-name" => "08:45 AM"
]
3 => array:2 [▼
"value" => "09:00"
"time-name" => "09:00 AM"
]
It's an array not object so you need to do {{ $slot['value'] }} not {{ $slot->value }}
Related
Hi i need to solve this case in session cart that have array in session
The size duplicated many times in blade like this
Size:
large
large
large
array:1 [▼ // app/Http/Controllers/Controller.php
1 => array:3 [▼
"id" => 1
"quantity" => 36
"color_items" => array:3 [▼
0 => array:2 [▼
"color" => "3"
"size" => "2"
]
1 => array:2 [▼
"color" => "2"
"size" => "2"
]
2 => array:2 [▼
"color" => "1"
"size" => "2"
]
]
]
]
<dl class="card-item-desc-1">
<dt>Size:</dt> #foreach($details['color_items'] as $de)
<dd>
{{ \App\Models\Size::find($de['size'][0])->name }}
</dd>
#endforeach
</dl>
You have a couple of options.
First strip the size out of the color_items array and use as its own array.
Second would be to use the power of collections
Change the foreach to
#foreach(collect($details['color_items'])->pluck('size')->unique() as $size)
<dd>
{{ \App\Models\Size::find($size)->name }}
</dd>
#endforeach
So what we are doing is adding the color_items to a collection, taking only the size field and then making sure its unique.
Hope that helps
I am following this tutorial:
https://learn.microsoft.com/en-us/graph/tutorials/php?tutorial-step=4
However, I did not manage to list all the events in my calendar. I was able to dump json file by using this:
return response()->json($events);
However, after replacing that line with:
$viewData['events'] = $events;
return view('calendar', $viewData);
I received this error:
ErrorException Undefined variable: dateRange (View:
C:\Users\syahm\FWIMS\resources\views\calendar.blade.php)
http://localhost:8000/calendar Hide solutions $dateRange is undefined
My controller.blade.php:
#extends('layout')
#section('content')
<h1>Calendar</h1>
<h2>{{ $dateRange }}</h2>
<a class="btn btn-light btn-sm mb-3" href={{action('CalendarController#getNewEventForm')}}>New event</a>
<table class="table">
<thead>
<tr>
<th scope="col">Organizer</th>
<th scope="col">Subject</th>
<th scope="col">Start</th>
<th scope="col">End</th>
</tr>
</thead>
<tbody>
#isset($events)
#foreach($events as $event)
<tr>
<td>{{ $event->getOrganizer()->getEmailAddress()->getName() }}</td>
<td>{{ $event->getSubject() }}</td>
<td>{{ \Carbon\Carbon::parse($event->getStart()->getDateTime())->format('n/j/y g:i A') }}</td>
<td>{{ \Carbon\Carbon::parse($event->getEnd()->getDateTime())->format('n/j/y g:i A') }}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
#endsection
My CalendarController.php:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
use App\TokenStore\TokenCache;
use App\TimeZones\TimeZones;
class CalendarController extends Controller
{
public function calendar()
{
$viewData = $this->loadViewData();
$graph = $this->getGraph();
// Get user's timezone
$timezone = TimeZones::getTzFromWindows($viewData['userTimeZone']);
// Get start and end of week
$startOfWeek = new \DateTimeImmutable('monday -1 week', $timezone);
$endOfWeek = new \DateTimeImmutable('monday', $timezone);
$queryParams = array(
'startDateTime' => $startOfWeek->format(\DateTimeInterface::ISO8601),
'endDateTime' => $endOfWeek->format(\DateTimeInterface::ISO8601),
// Only request the properties used by the app
'$select' => 'subject,organizer,start,end',
// Sort them by start time
'$orderby' => 'start/dateTime',
// Limit results to 25
'$top' => 25
);
// Append query parameters to the '/me/calendarView' url
$getEventsUrl = '/me/calendarView?'.http_build_query($queryParams);
$events = $graph->createRequest('GET', $getEventsUrl)
// Add the user's timezone to the Prefer header
->addHeaders(array(
'Prefer' => 'outlook.timezone="'.$viewData['userTimeZone'].'"'
))
->setReturnType(Model\Event::class)
->execute();
$viewData['events'] = $events;
return view('calendar', $viewData);
}
private function getGraph(): Graph
{
// Get the access token from the cache
$tokenCache = new TokenCache();
$accessToken = $tokenCache->getAccessToken();
// Create a Graph client
$graph = new Graph();
$graph->setAccessToken($accessToken);
return $graph;
}
}
Added 'dd($viewData);' to my calendarcontroller.php and I received this:
array:4 [▼
"userName" => "ABC"
"userEmail" => "ABC#localhost"
"userTimeZone" => "Eastern Standard Time"
"events" => array:3 [▼
0 => Microsoft\Graph\Model\Event {#310 ▼
#_propDict: array:6 [▼
"#odata.etag" => "W/"3ixz55KajUefR0tOGmhiSQABIRK5QA==""
"id" => "AAMkAGZlZWE2N2ZlLTczM2ItNDI2Ny05NWU0LTZmOWFiNzZmZmJhZABGAAAAAAC85uAHa4F5RpnXBxFnN2zgBwDeLHPnkpqNR59HS04aaGJJAAAAAAENAADeLHPnkpqNR59HS04aaGJJAAEhezxbAAA="
"subject" => "AA 2"
"start" => array:2 [▼
"dateTime" => "2020-11-23T09:30:00.0000000"
"timeZone" => "Eastern Standard Time"
]
"end" => array:2 [▼
"dateTime" => "2020-11-23T16:30:00.0000000"
"timeZone" => "Eastern Standard Time"
]
"organizer" => array:1 [▼
"emailAddress" => array:2 [▼
"name" => "AA"
"address" => "AA#localhost"
]
]
]
}
1 => Microsoft\Graph\Model\Event {#296 ▼
#_propDict: array:6 [▼
"#odata.etag" => "W/"3ixz55KajUefR0tOGmhiSQABIl5/QA==""
"id" => "AAMkAGZlZWE2N2ZlLTczM2ItNDI2Ny05NWU0LTZmOWFiNzZmZmJhZABGAAAAAAC85uAHa4F5RpnXBxFnN2zgBwDeLHPnkpqNR59HS04aaGJJAAAAAAENAADeLHPnkpqNR59HS04aaGJJAAEhezxcAAA="
"subject" => "AA 3"
"start" => array:2 [▼
"dateTime" => "2020-11-25T09:30:00.0000000"
"timeZone" => "Eastern Standard Time"
]
"end" => array:2 [▼
"dateTime" => "2020-11-25T16:30:00.0000000"
"timeZone" => "EasternStandard Time"
]
"organizer" => array:1 [▼
"emailAddress" => array:2 [▶]
]
]
}
2 => Microsoft\Graph\Model\Event {#306 ▼
#_propDict: array:6 [▼
"#odata.etag" => "W/"3ixz55KajUefR0tOGmhiSQABIwjzEw==""
"id" => "AAMkAGZlZWE2N2ZlLTczM2ItNDI2Ny05NWU0LTZmOWFiNzZmZmJhZABGAAAAAAC85uAHa4F5RpnXBxFnN2zgBwDeLHPnkpqNR59HS04aaGJJAAAAAAENAADeLHPnkpqNR59HS04aaGJJAAEjdLLVAAA="
"subject" => "AA 4/2020 "
"start" => array:2 [▼
"dateTime" => "2020-11-26T14:30:00.0000000"
"timeZone" => "Eastern Standard Time"
]
"end" => array:2 [▼
"dateTime" => "2020-11-26T16:00:00.0000000"
"timeZone" => "Eastern Standard Time"
]
"organizer" => array:1 [▼
"emailAddress" => array:2 [▼
"name" => "AA"
"address" => "AA#localhost"
]
]
]
}
]
]
Removed this from my calendar.blade.php and it is now working!
<h2>{{ $dateRange }}</h2>
<a class="btn btn-light btn-sm mb-3" href={{action('CalendarController#getNewEventForm')}}>New event</a>
I have a ecommerce website which shows multiple variants like size, color etc...I have create a products details array as below.
array:1 [▼
0 => array:12 [▼
"proId" => 2268
"name" => "PAMPERS BABY DRY PANTS LARGE -64 PANTS"
"oprice" => 1099.0
"ofprice" => 1044.05
"slug" => "pampers-pants-large-64s-9-14kg"
"variants" => array:5 [▼
0 => array:2 [▼
"title" => "NOS"
"items" => array:1 [▼
0 => array:7 [▼
"items_id" => 567
"items_value" => "42 NOS"
"sku" => "1058898"
"ean" => "4902430645577"
"mrp" => 699.0
"mfp" => 664.05
"combo" => ""
]
]
]
1 => array:2 [▼
"title" => "NOS"
"items" => array:1 [▼
0 => array:7 [▼
"items_id" => 568
"items_value" => "24 pants"
"sku" => "PM50"
"ean" => "4902430900485"
"mrp" => 399.0
"mfp" => 379.05
"combo" => ""
]
]
]
2 => array:2 [▶]
3 => array:2 [▶]
4 => array:2 [▼
"title" => "Weight"
"items" => array:1 [▼
0 => array:7 [▼
"items_id" => 591
"items_value" => "100gm"
"sku" => "wet"
"ean" => "346"
"mrp" => 436.0
"mfp" => 33.0
"combo" => ""
]
]
]
]
]
]
The code for the above is,
$product = Products::select('products.*', 'brand.name', 'category.id as cat_id')->join('category', 'category.id', '=', 'products.category_id')->join('subcategory', 'subcategory.id', '=', 'products.subcategory_id')->join('brand', 'brand.id', '=', 'products.brand_id', 'left')->where(array('products.slug' => $slug, 'products.status' => 1))->get();
$datas = [];
foreach ($product as $pro) {
$item = [
"proId" => $pro->id,
"name" => $pro->title,
"oprice" => $pro->org_price,
"ofprice" => $pro->off_price,
"slug" => $pro->slug,
"variants" => []
];
$variantlist = Provariants::select('products_variants.id', 'variants.name', 'products_variants.variants_type_id', 'products_variants.sku', 'products_variants.ean', 'products_variants.price', 'products_variants.mfp', 'products_variants.combo')->join('variants_type', 'variants_type.id', '=', 'products_variants.variants_type_id')->join('variants', 'variants.id', '=', 'variants_type.variants_id')->where(array('products_variants.products_id' => $pro->id, 'products_variants.status' => 1))->get();
foreach ($variantlist as $list) {
$typelist = Variantstype::where(array('id' => $list->variants_type_id, 'status' => 1))->get();
$subitems = [];
foreach ($typelist as $type) {
$subitems[] = [
"items_id" => $list->id,
"items_value" => $type->name,
"sku" => $list->sku,
"ean" => $list->ean,
"mrp" => $list->price,
"mfp" => $list->mfp,
"combo" => $list->combo
];
}
$item['variants'][] = [
"title" => $list->name,
"items" => $subitems,
];
}
$datas[] = $item;
}
Now the problem is, the title is showing multiple times and one items displaying under it. To get an idea check below image.
https://i.stack.imgur.com/tLjc9.png
I want title to be shown once and all the items under each title should be place together. Please check the required output.
https://i.stack.imgur.com/RFddf.png
Current result code is,
#if (count($pro['variants']) > 0)
#foreach ($pro['variants'] as $variants)
<div class="variants mt-4">
<div>
<h6>{{ $variants['title'] }}</h6>
#foreach ($variants['items'] as $var_items)
<p>{{ $var_items['items_value'] }}</p>
#endforeach
</div>
</div>
#endforeach
#endif
How can I achieve the required output?
A similar solution to the one by #sta, but I think this might be a little more flexible.
#php
$title = '';
#endphp
#foreach ($pro['variants'] as $variant)
<div class="variants mt-4">
<div>
#if($title !== $variant['title'])
<h6>{{ $variant['title'] }}</h6>
#endif
#foreach($variant['items'] as $var_item)
<p>{{ $var_item['items_value'] }}</p>
#endforeach
</div>
</div>
#php
$title = $variant['title'];
#endphp
#endforeach
I have not tested this, but it should write out the title every time $variant['title'] changes.
This will work :
#php
$x = 1;
#endphp
#foreach ($pro['variants'] as $variants)
<div class="variants mt-4">
<div>
#if($x == 1)
<h6>{{ $variants['title'] }}</h6>
#endif
#php
$x += $x;
#endphp
#foreach ($variants['items'] as $var_items)
<p>{{ $var_items['items_value'] }}</p>
#endforeach
</div>
</div>
#endforeach
Above code tested here
I have cloned HTML prepared with input group. Some checkbox and radio are not selected. I want to push into the selected email.
I've tried many methods.
array_merge()
array_combine()
array_push()
array_merge_recursive()
But none of these worked. Or I don't know how to get them to work.
HTML
<div class="form-group">
<label for="name">E-posta</label>
<div class="input-group">
<input type="email" class="form-control email-address" name="email[]" placeholder="E-Posta giriniz">
<div class="btn-group">
<label class="btn btn-default">
<input class="primary-radio" type="radio" name="primary[]" checked autocomplete="off"> <span
class="fas fa-star"></span>
</label>
<label class="btn btn-default">
<input class="ban-checkbox" type="checkbox" name="ban[]" autocomplete="off"> <span
class="fas fa-ban"></span>
</label>
<label class="btn btn-default">
<input class="invalid-checkbox" type="checkbox" name="invalid[]" autocomplete="off"> <span
class="fas fa-exclamation-circle"></span>
</label>
</div>
</div>
</div>
PHP
public function add(Request $request)
{
$emails = $request->input('email');
$primary = $request->input('primary');
$ban = $request->input('ban');
$invalid = $request->input('invalid');
$emailAddresses = array();
foreach ($emails as $emailKey => $emailValue) {
$emailAddresses[$emailKey] = [
'emailAddress' => $emailValue,
'invalid' => $invalid,
'lower' => $emailAddresses,
'ban' => $ban,
'primary' => $primary
];
}
dd($emailAddresses);
}
Output
array:3 [▼
0 => array:5 [▼
"emailAddress" => "dodis#live.com"
"invalid" => null
"lower" => "dodis#live.com"
"ban" => array:2 [▼
0 => "on"
1 => "on"
]
"primary" => array:1 [▼
0 => "on"
]
]
1 => array:5 [▼
"emailAddress" => "test#live.com"
"invalid" => null
"lower" => "test#live.com"
"ban" => array:2 [▼
0 => "on"
1 => "on"
]
"primary" => array:1 [▼
0 => "on"
]
]
2 => array:5 [▼
"emailAddress" => "bundayok#live.com"
"invalid" => null
"lower" => "bundayok#live.com"
"ban" => array:2 [▼
0 => "on"
1 => "on"
]
"primary" => array:1 [▼
0 => "on"
]
]
]
Such an output comes. It shouldn't be like that. Here's the example I want it to be.
0: {
emailAddress: "bilgi#ekinciler.com.tr"
invalid: false
lower: "bilgi#ekinciler.com.tr"
optOut: false
primary: true
}
1: {
emailAddress: "muhasebe#ekinciler.com"
invalid: false
lower: "muhasebe#ekinciler.com"
optOut: false
primary: false
}
I am sorry for my English. I hope I can. I would be glad if you help.
Try below code,
What I have done is added array index to each loop to get correct value of that index, here your
"ban" => array:2 [▼
0 => "on"
1 => "on"
]
is array itself so you need 0 index for the first array data. and same applies for all the array.
public function add(Request $request)
{
$emails = $request->input('email');
$primary = $request->input('primary');
$ban = $request->input('ban');
$invalid = $request->input('invalid');
$emailAddresses = array();
$i = 0;
foreach ($emails as $emailKey => $emailValue) {
$emailAddresses[$emailKey] = [
'emailAddress' => $emailValue,
'invalid' => $invalid[$i]?$invalid[$i]:'',
'lower' => $emailAddresses,
'ban' => $ban[$i]?$ban[$i]:'',
'primary' => $primary[$i]?$primary[$i]:''
];
$i++;
}
dd($emailAddresses);
}
I have completely changed the format of my array now, and I had what I hope is a simple misunderstanding on my part. So my array now looks like the following
array:9 [▼
0 => array:4 [▼
"leadData" => array:7 [▼
"LeadID" => "1232806"
"Client" => "Some Client"
"LeadName" => "Test"
"Owner" => "Someone"
"Value" => "2160.00"
]
"clientData" => array:2 [▼
"Prospect" => "No"
]
"quoteData" => array:8 [▼
"QuoteID" => "Q0020"
"ProjectName" => "Test"
"Amount" => "1234"
]
"customData" => array:2 [▼
0 => array:1 [▼
"Type" => "New"
]
1 => array:1 [▼
"Month" => "June 16"
]
]
]
2 => array:4 [
...
]
]
So it is essentially now 4 inner arrays. Now if I do the following, I can print out all the data for the leadData inner array
foreach($leadArray as $array)
<tr>
foreach($array['leadData'] as $leadKey => $leadData)
<td>
{{ $leadData }}
</td>
endforeach
</tr>
endforeach
That works fine. However, I only want to display certain parts of this array. I would have presumed doing something like the following would work
foreach($leadArray as $array)
<tr>
foreach($array['leadData'] as $leadKey => $leadData)
<td>
{{ $leadData['LeadID'] }}
</td>
<td>
{{ $leadData['LeadName'] }}
</td>
endforeach
</tr>
endforeach
However if I do this I get and Illegal String Offset error. Is this not how I would access this data?
p.s. Ignore the way I do the foreach loop etc, this is because I am using a template engine.
Thanks
You don't have to loop over the second array, you can use the keys to get the data.
foreach($leadArray as $array)
<tr>
<td>
{{ $array['leadData']['LeadID'] }}
</td>
<td>
{{ $array['leadData']['LeadName'] }}
</td>
</tr>
endforeach
Your main array will be like this, where you want to work, right?
<pre>
$aMainArray = array(
0 => array(
"leadData" => array(
"LeadID" => "1232806",
"Client" => "Some Client",
"LeadName" => "Test",
"Owner" => "Someone",
"Value" => "2160.00",
)
)
);
foreach ($aMainArray AS $aSubArray) {
print_r($aSubArray);
// You can echo your required values like below
echo $aSubArray['leadData']['LeadID'];
echo $aSubArray['leadData']['LeadName'];
// OR like this one
foreach ($aSubArray AS $value) {
echo $value['LeadID'];
echo $value['LeadName'];
}
}
</pre>