Laravel 5.4 call to undefined function Session::getMetadataBag() - laravel

In version 5.3, Session::getMetadataBag() was working good to fetch the Session data but after upgrade it is throwing error as Call to undefined method Illuminate\Session\Store::getMetadataBag()
Session::all() doesn't carry last use record like below:
array:4 [
"_token" => "SQjQpOxCec5lal5tzFgW1FMtrcOaaIqjoii2fuCb"
"_previous" => array:1 [
"url" => "http://my-url"
]
"_flash" => array:2 [
"old" => []
"new" => []
]
"lang" => "en"
]
while Session::getMetaDataBag carries below record:
MetadataBag {#582
-name: "__metadata"
-storageKey: "_sf2_meta"
#meta: & array:3 [
"u" => 1485527838
"c" => 1485527838
"l" => "0"
]
-lastUsed: 1485527838
-updateThreshold: 0
}
Any alternative to get this lastUsed record in Laravel 5.4?

The metadatabag is actually part of Symfony. In Laravel 5.4, the session component was refactored to not depend on Symfony anymore, so the metadatabag is gone. You will need to implement this functionality yourself.

Related

Laravel : Countries with Laravel Analytics

I'm using Laravel Analytics to get data of the visitors of my application.
In my Google Analytics dashboard, every page have it own visits numbers, unique visitors, countries of visitors etc .. like in this image :
In my web.php, I'm creating a route to test the package :
Route::get('/data', function () {
$analyticsData = Analytics::fetchMostVisitedPages(Period::days(7));
dd($analyticsData);
});
This route returns :
Illuminate\Support\Collection {#1564 ▼
#items: array:3 [▼
0 => array:3 [▼
"url" => "/new"
"pageTitle" => "test"
"pageViews" => 1534
]
1 => array:3 [▼
"url" => "/"
"pageTitle" => "test"
"pageViews" => 450
]
2 => array:3 [▼
"url" => "/customize/8"
"pageTitle" => "test"
"pageViews" => 196
]
As you can see the returned array have only url, pageTitle and pageViews. How can I add additional informations in the returned array such as countries or geographic localisation as shown in the first image ?
I've never used the package, but reading the docs, something like this will do.
The package docs state you can use any query you want, and the Google Analytics docs give an example of getting session by location.
public function myCustomMethod($maxResults = 20)
{
$response = $this->performQuery(
$period,
'ga:sessions', // metrics
[
'dimensions' => 'ga:country',
'sort' => '-ga:sessions',
'max-results' => $maxResults,
],
);
return collect($response['rows'] ?? [])->map(fn (array $pageRow) => [
// Do something with the rows that are returned.
// I'm not sure how they're returned from the main response.
]);
}
Note, this is completely untested, you might want to fiddle with some of the data here.
Assuming I understand the documentation, this will get all countries (dimensions), it will use sessions (ga:sessions from second parameter) to measure the countries data.
It'll then just sort and get a maximum number of results.
You could change the metrics to ga:pageviews, but it's ultimately down to you what queries you want to use.
I've linked the documentation so you can find them out yourself.
You're using spatie/laravel-analytics package.
As you can see here: spatie laravel analytics - Analytics.php the fetchMostVisitedPages method that you are calling only returns that data.
Please take a look in github to see more information about this package.
fetchMostVisitedPages method:
public function fetchMostVisitedPages(Period $period, int $maxResults = 20): Collection
{
$response = $this->performQuery(
$period,
'ga:pageviews',
[
'dimensions' => 'ga:pagePath,ga:pageTitle',
'sort' => '-ga:pageviews',
'max-results' => $maxResults,
],
);
return collect($response['rows'] ?? [])->map(fn (array $pageRow) => [
'url' => $pageRow[0],
'pageTitle' => $pageRow[1],
'pageViews' => (int) $pageRow[2],
]);
}

Laravel Microsoft Graph request how to solve Cannot use object of type?

I try to do a graph request in my laravel project to connect Microsoft API. All works fine, but I don't find a way to get single data from my request.
$info = $graph->createRequest("GET", "/groups/{id}/calendar/events?\$filter=start/dateTime ge '" . $todayDate . "T00:00' and end/dateTime lt '" . $todayDate . "T23:00'")
->addHeaders(array("Content-Type" => "application/json"))
->setReturnType(\Microsoft\Graph\Model\User::class)
->setTimeout("1000")
->execute();
Output is a large json with all information. If I just want to get $info[0]['attendees'];
I get error: Cannot use object of type Microsoft\Graph\Model\User as array in file
Why? And how to access? If I try load object like $info->attendees I got error: Trying to get property 'attendees' of non-object.
This is my output (not all) of dd($info) it includes attendees as array like "id"
array:10 [▼
0 => Microsoft\Graph\Model\Event {#1532 ▼
#_propDict: array:43 [▼
"#odata.etag" => "{tag}""
"id" => "{id}"
"createdDateTime" => "2021-03-19T10:36:08.0812445Z"
"lastModifiedDateTime" => "2021-03-19T10:36:11.9402917Z"
"attendees" => array:1 [▼
0 => array:3 [▼
"type" => "required"
"status" => array:2 [▶]
"emailAddress" => array:2 [▼
"name" => "name"
"address" => "mail"
]
]
]
It seems your array is returning an array of attendees, you have an extra level in your array. So to fetch the first attendees status, you need to use the following code:
Try this
foreach($info as $item){
foreach($item as $inf ){
$a = $inf->status
dd($a)
}
}

Laravel 5.7 - forget or except from collection when key could vary

Laravel's except() and forget() helpers both do what they do by key. I'm wondering if there's a slick laravel way to use either of those to exclude an item when the key is unpredictable. I have a collection that looks like this:
Collection {#469
#items: array:3 [
0 => array:2 [
"name" => "Bug"
"class" => "App\Models\BugTypes\Bug"
]
1 => array:2 [
"name" => "PreBug"
"class" => "App\Models\BugTypes\PreBug"
]
2 => array:2 [
"name" => "Request"
"class" => "App\Models\BugTypes\Request"
]
]
}
and I want to always exclude PreBug. The issue is that the collection is built dynamically from namespaces and if you were to add or remove a namespace, the key for PreBug could change. Namely if you were to create a type that alphabetically precedes the letter P. So while $data->except(1); works now, I cannot rely on it. I know I could filter or map to remove it, so mostly just wondering if someone sees a more creative solution than that.

Facebook marketing API insights (Destination url)

I'm trying to get data using Facebook Marketing API.
$api = FacebookAds::init('TOKEN');
$start = Carbon::create(2018,11,16);
$end = Carbon::create(2018,11,16);
$period = Period::create($start,$end);
$in = $api->insights($period,'act_ID', 'ad',[
'fields' => ['impressions', 'objective', 'actions'....]
]);
I'm getting each ad actions like this:
"actions" => array:10 [▼
0 => array:2 [▼
"action_type" => "comment"
"value" => "1"
]
1 => array:2 [▼
"action_type" => "offsite_conversion.fb_pixel_purchase"
"value" => "1"
]
2 => array:2 [▼
"action_type" => "photo_view"
"value" => "114"
]....
]
My question is how to get each ad destination URL?
Thanks
You actually have to use AdCreative information to get the destination URL: https://developers.facebook.com/docs/marketing-api/reference/ad-creative/
I use the object_story_spec and match the AdCreative with the Ad by id ( they share the same ID ).
https://graph.facebook.com/v14.0/'adcreativeid'/?fields=object_story_spec
You can use this code. Destination URL will mentioned in link data -> link

Replicating models across 2 systems

I have 2 systems which are seperate from each other. To let them communicate I have built an API. Both systems have common Models, one of which is a Project Model with all its relationships.
Within System A, to send a Project with its relationships I do the following.
$client = new GuzzleHttp\Client();
$jsonProject = json_encode(Project::with('projectType', 'projectType.projectTypeData',
'projectAssets', 'projectAssets.projectAssetsData')->find($project->id));
$req = $client->request('POST', 'https://someurl/postProject', [
'body' => json_encode($jsonProject),
'headers' => [
'Content-Type' => 'application/json',
'Content-Length' => strlen($jsonProject),
]
]);
Within System B I have the routes set up, and when the above is posted the following is triggered
public function store(Request $request)
{
$project = $request->all();
dd($project);
}
When I make the post request from System A I see something like this because of the dump in System B (I have removed a lot of the output to cut down on code).
array:17 [
"id" => 3
"projectName" => "Test Project"
"user_id" => 1
"contact" => "John Doe"
"project_type" => array:7 [
"id" => 3
"project_id" => 3
"project_type_data" => array:1 [
0 => array:8 [
"id" => 5
"projectType" => "Standard"
]
]
]
"project_assets" => array:7 [
"id" => 2
"project_id" => 3
"project_assets_data" => array:4 [
0 => array:8 [
"id" => 5
"label" => "Logo"
"altTag" => ""
"urlPath" => ""
"projectAssetsId" => 2
]
]
]
]
So everything seems to work fine. My question is this. System B now has a load of json data containing all the data required to make the models. If I was able to send a model via the api (not json) then I could easily create the model within System B. Because it is json data however, and I cant decode it because it is not a string, do I have to start looping it all in order to make my models?

Resources