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>
Related
I am using Elasticsearch to get data.
$addresses = Address::multiMatchSearch()
->fields(['full_address'])
->query("nsw")
->fuzziness('AUTO')
->paginate();
foreach ($addresses as $address) {
dd($address);
}
I am receiving the object in the following format. How can access city attribute from object ? As you can see that I dd inside a loop. This will be the first record.
ElasticScoutDriverPlus\QueryMatch {#781 ▼
-lazyModelFactory: ElasticScoutDriverPlus\Factories\LazyModelFactory {#779 ▶}
-hit: ElasticAdapter\Search\Hit {#710 ▼
-hit: array:5 [▼
"_index" => "addresses"
"_type" => "_doc"
"_score" => 5.3255396
"_source" => array:25 [▼
"id" => 228606
"company_name" => "Lucy Segal"
"customer_name" => null
"street_address" => "19 Russell Road"
"suburb" => "New Lambton"
"city" => null
"postcode" => 2305
"state" => "NSW"
"telephone" => "400473458"
"status" => 1
"geocoded" => 1
"country" => "Australia"
"full_address" => "Lucy Segal, 19 Russell Road, New Lambton, NSW, 2305"
]
]
}
}
im using laravel 7 and trying to get datas from api use http.
when i use dd('$datas'),i got this
array:1 [▼"manajemen_sdm" => array:4 [▼ 0 => array:9 [▼ "id_karyawan" => "2" "nama" => "Koko Hendriko" "gender" => "Laki-Laki" "ttl" => "1998-04-07" "alamat" => "Kp.Palasari " "no_telp" => "821863141" "email" => "kokohendriko#gmail.com" "npwp" => "89678923652" "golongan" => "B" ] 1 => array:9 [▶] 2 => array:9 [▶] 3 => array:9 [▶] ] ]
my controller
public function index()
{
$datas = Http::get('https://projectsoadenis.000webhostapp.com/api')->json();
return view('sdm.index',compact('datas'));
}
view
#foreach ($datas as $datasdm)
<tr>
<td>{{$no++}}</td>
<td>{{$datasdm['id_karyawan']}}</td>
<td>{{$datasdm['nama']}}</td>
<td>{{$datasdm['gender']}}</td>
<td>{{$datasdm['ttl']}}</td>
<td>{{$datasdm['alamat']}}</td>
<td>{{$datasdm['no_telp']}}</td>
<td>{{$datasdm['email']}}</td>
<td>{{$datasdm['npwp']}}</td>
<td>{{$datasdm['golongan']}}</td>
</tr>
#endforeach
If you look at the data you are getting back from the API, your data is actually nested inside objects that live under a manajemen_sdm property.
Your loop should work correctly if you update it to loop over the manajemen_sdm key:
#foreach ($datas['manajemen_sdm'] as $datasdm)
<tr>
<td>{{$no++}}</td>
<td>{{$datasdm['id_karyawan']}}</td>
<td>{{$datasdm['nama']}}</td>
<td>{{$datasdm['gender']}}</td>
<td>{{$datasdm['ttl']}}</td>
<td>{{$datasdm['alamat']}}</td>
<td>{{$datasdm['no_telp']}}</td>
<td>{{$datasdm['email']}}</td>
<td>{{$datasdm['npwp']}}</td>
<td>{{$datasdm['golongan']}}</td>
</tr>
#endforeach
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 want to store data from a form into two tables on database when the radio button value on my view is "new", but the below problem was happened. But if the value is "existing", it's works fine.
what wrong with my code ?
Array to string conversion (SQL: insert into customers (company_name, address, service_id, tc_name, tc_dept, tc_phone, tc_email, bill_name, bill_dept, bill_phone, bill_email, updated_at, created_at) values (PT Bank ABC, JL Sudirman, 1, Budi, Technical, 0812345678, budi#abc.co.id, Joko, Finance, 08123456789, joko#abc.co.id, 2016-12-14 11:21:26, 2016-12-14 11:21:26))
here my store code
if($request->select_data == 'new'){
$customer = New Customer;
$customer->company_name = $request->company_name;
$customer->address = $request->address;
$customer->service_id = $request->service_id;
$customer->tc_name = $request->tc_name;
$customer->tc_dept = $request->tc_dept;
$customer->tc_phone = $request->tc_phone;
$customer->tc_email = $request->tc_email;
$customer->bill_name = $request->bill_name;
$customer->bill_dept = $request->bill_dept;
$customer->bill_phone = $request->bill_phone;
$customer->bill_email = $request->bill_email;
$customer->save();
$salesorder = New SalesOrder;
$salesorder->pid = $request->pid;
$salesorder->project_name = $request->project_name;
$salesorder->customer_id = $request->company_id;
$salesorder->total = $request->totalfee;
$salesorder->status = 'submit';
$salesorder->detail = $request->detail;
$salesorder->save();
}else{
$salesorder = New SalesOrder;
$salesorder->pid = $request->pid;
$salesorder->project_name = $request->project_name;
$salesorder->customer_id = $request->company_id;
$salesorder->total = $request->totalfee;
$salesorder->status = 'submit';
$salesorder->detail = $request->detail;
$salesorder->save();
//dd($salesorder);
}
dd($request->all()); result
array:32 [▼
"sales_order_id" => "9"
"select_data" => "new"
"company_id" => "2"
"company_name" => "PT Bank ABC"
"address" => "JL Sudirman"
"tc_name" => "Budi"
"tc_dept" => "Technical"
"tc_phone" => "0812345678"
"tc_email" => "budi#abc.co.id"
"bill_name" => "Joko"
"bill_dept" => "Finance"
"bill_phone" => "08123456789"
"bill_email" => "joko#abc.co.id"
"pid" => "PID002"
"project_name" => "Implementasi"
"order_identifier" => array:2 [▶]
"service_name" => array:2 [▶]
"service_id" => array:2 [▶]
"order_type" => array:2 [▶]
"select_plan" => array:2 [▶]
"qty" => array:2 [▶]
"unit_price" => array:2 [▶]
"total_price" => array:2 [▶]
"note" => array:2 [▶]
"emp_name" => array:1 [▶]
"emp_id" => array:1 [▶]
"position" => array:1 [▶]
"position_id" => array:1 [▶]
"mandays" => array:1 [▶]
"detail" => "Coba Coba"
"totalfee" => "3100"
"_token" => "uxmXBwJKHWIoXDSFetU4oRgTiTftYEhhdpx4CaPr"
]
radio button html code
<div class="row-fluid select_data">
<input name="select_data" id="select_data" type="radio" value="new">
<span class="lbl">New</span>
<input name="select_data" id="select_data" type="radio" value="existing">
<span class="lbl">Existing</span>
</div>
The problem is because some of $request properties are arrays. You can't store an array in integer, string etc columns. If you want to store them as arrays, you can serialize them:
$customer->service_id = json_encode(service_id);
And store in text or json columns:
$table->json('service_id');
$table->text('service_id');
Try to change id of both the radio buttons, you can not set same id for different elements, it will behave differently
Try this one
<input name="select_data" name="select_data" type="radio" value="new">
<span class="lbl">New</span>
<input name="select_data" name="select_data" type="radio" value="existing">
<span class="lbl">Existing</span>
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>