So I'm using the following array to pass in $record params , but I'm getting errors when a particular return from the API is null.
Here is the array:
$office->load([
'post_title' => $record->office,
'location_id' => $record->location_id,
'location' => $record->location,
'business_unit' => $record->business_unit,
'type_id' => $record->type_id,
'brand_id' => $record->brand_id,
]);
My $record->brand_id is being returned as null and it crashes my whole script, is there a way that I can output null as a string and wrap the $record->brand_id in something?
Update:
I ended up configuring the needed brand_id as $record->brand_id ?? $default_value which worked great!
If you know which values may be null and have no impact on API output you can try operators checking null values, like null coalescing operator (??).
$default_value = 'default';
$office->load([
'post_title' => $record->office,
'location_id' => $record->location_id,
'location' => $record->location,
'business_unit' => $record->business_unit,
'type_id' => $record->type_id,
'brand_id' => $record->brand_id ?? $default,
]);
Related
I do have a resource (DeviceResource) in my Laravel API which contains another resource (SimcardResource). There is a OnetoOne relationship between those resources but sometimes a device has no associated simcard.
If this is the case my DeviceResource returns for the simcard null instead of an empty json object.
I do need an empty json object because I present information called from my API in my Vue frontend by accessing an object e.g. like device.simcard.phone_number
My DeviceResource class looks like this:
public function toArray($request)
{
return [
'id' => $this->resource->id,
'model' => $this->resource->model,
'device_name' => $this->resource->device_name,
'operating_system' => $this->resource->operating_system,
'os_version' => $this->resource->os_version,
'emei' => $this->resource->emei,
'device_password' => $this->resource->device_password,
'purchase_date' => $this->resource->purchase_date,
'associated_worker' => $this->resource->associated_worker,
'remarks' => $this->resource->remarks,
'device_status_id' => $this->resource->device_status_id,
// 'simcard' => $this->resource->simcard ?: (object)[],
'simcard' => SimcardResource::make($this->whenLoaded('simcard')) ?: (object)[],
];
}
The commented section:
'simcard' => $this->resource->simcard ?: (object)[]
Works perfectly but returns all fields from my simcard table but I only need fields defined in my SimcardResource class so I tried the following:
'simcard' => SimcardResource::make($this->whenLoaded('simcard')) ?: (object)[]
But it still returns null instead of an empty json object.
Okay maybe its not the best solution but my DeviceResource class now looks like this:
public function toArray($request)
{
if (is_null($this->resource->simcard)) {
return [
'id' => $this->resource->id,
'model' => $this->resource->model,
'device_name' => $this->resource->device_name,
'operating_system' => $this->resource->operating_system,
'os_version' => $this->resource->os_version,
'emei' => $this->resource->emei,
'device_password' => $this->resource->device_password,
'purchase_date' => $this->resource->purchase_date,
'associated_worker' => $this->resource->associated_worker,
'remarks' => $this->resource->remarks,
'device_status_id' => $this->resource->device_status_id,
'simcard' => (object) [],
];
} else {
return [
'id' => $this->resource->id,
'model' => $this->resource->model,
'device_name' => $this->resource->device_name,
'operating_system' => $this->resource->operating_system,
'os_version' => $this->resource->os_version,
'emei' => $this->resource->emei,
'device_password' => $this->resource->device_password,
'purchase_date' => $this->resource->purchase_date,
'associated_worker' => $this->resource->associated_worker,
'remarks' => $this->resource->remarks,
'device_status_id' => $this->resource->device_status_id,
// 'simcard' => $this->resource->simcard ?: (object)[],
'simcard' => SimcardResource::make($this->whenLoaded('simcard')),
];
}
}
It is Laravel resource default behaviour that if you do not have any data than resource will also return you the null resource object. You have to manage it yourself in other way like by defining each parameter has null value that's it.
Laravel introduce best ways,for example whenLoaded,but try this
... ?? json_encode(new stdClass)
im working with lravel 7 project
before i used to work with xampp 7.3.21 / PHP 7.3.21 and everything is so good
ive upgraded to xampp 7.4.9 / PHP 7.4.9 and i get
Trying to access array offset on value of type null
in the most of my porject
and this is example
public function show
(
$id,
// selects
$selects_names,
$selects_languages,
$selects_models,
$selects_policies,
$selects_types,
$selects_ranks,
// end selects
)
{
return view('curd.show',compact
(
'id'
// select
'selects_names',
'selects_languages',
'selects_models',
'selects_policies',
'selects_types',
'selects_ranks',
// end selects
));
}
and this is the blade code
#if($selects_names)
#foreach($selects_names as $key => $selects_name)
#include(
'treats.show_selects',
[
'name' => $selects_name,
'language' => $selects_languages[$key],
'model' => $selects_models[$key],
'policy' => $selects_policies[$key] ?? null,
'show_type' => $selects_types[$key],
'rank' => $selects_ranks[$key] ?? null,
]
)
#endforeach
#endif
and always get the above error
most of my program is included from the code above they are treate function and now most of it now working
Some key in $select_names is probably not in the other arrays.
Define a default value for the other arrays as you did for $select_policies and $select_ranks:
[
'name' => $selects_name,
'language' => $selects_languages[$key] ?? '',
'model' => $selects_models[$key] ?? null,
'policy' => $selects_policies[$key] ?? null,
'show_type' => $selects_types[$key] ?? null,
'rank' => $selects_ranks[$key] ?? null,
]
I am currently testing the facebook marketing API to use it to get data from campaigns for a custom dashboard.
I am able to get data out from the ad account, but for some, for me, unknown reason a am getting Call to a member function getData() on a non-object while getting data. It will output some data before exiting.
I am testing with this
if($_SESSION["facebook_access_token"]) {
Api::init(
'XXXXXXXXXX',
'XXXXXXXXXXXXXXXXX',
$_SESSION["facebook_access_token"]
);
$account = new AdAccount('act_XXXXXXXXXXXXX');
$campaigns = $account->getCampaigns(array(
CampaignFields::NAME,
CampaignFields::OBJECTIVE,
CampaignFields::EFFECTIVE_STATUS,
CampaignFields::CONFIGURED_STATUS
), array(
CampaignFields::EFFECTIVE_STATUS => array(
ArchivableCrudObjectEffectiveStatuses::ACTIVE
),
));
foreach($campaigns as $campaignset) {
$cid = $campaignset->getData()['id'];
$campaign = new Campaign($cid);
$insights = $campaign->getInsights(array(
InsightsFields::CAMPAIGN_NAME,
InsightsFields::ADSET_NAME
));
var_dump($insights->current()->getData());
}
}
It will output this
array (size=48)
'account_id' => null
'account_name' => null
'action_values' => null
'actions' => null
'actions_per_impression' => null
'ad_id' => null
'ad_name' => null
'adset_id' => null
'adset_name' => null
'call_to_action_clicks' => null
'campaign_id' => null
'campaign_name' => string 'Svendborg - Mødsparnord – kopi' (length=33)
'cost_per_action_type' => null
'cost_per_total_action' => null
'cost_per_unique_click' => null
'cost_per_inline_link_click' => null
'cost_per_inline_post_engagement' => null
'cpm' => null
'cpp' => null
'ctr' => null
'date_start' => string '2015-11-05' (length=10)
'date_stop' => string '2015-12-07' (length=10)
'frequency' => null
'impressions' => null
'inline_link_clicks' => null
'inline_post_engagement' => null
'product_id' => null
'reach' => null
'relevance_score' => null
'social_clicks' => null
'social_impressions' => null
'social_reach' => null
'spend' => null
'total_action_value' => null
'total_actions' => null
'total_unique_actions' => null
'unique_clicks' => null
'unique_ctr' => null
'unique_social_clicks' => null
'video_avg_pct_watched_actions' => null
'video_avg_sec_watched_actions' => null
'video_complete_watched_actions' => null
'video_p100_watched_actions' => null
'video_p25_watched_actions' => null
'video_p50_watched_actions' => null
'video_p75_watched_actions' => null
'video_p95_watched_actions' => null
'website_ctr' => null
and some other campaigns, but it will always stop with the above mentioned error at the same spot every time. But I cannot see what's wrong.
This is because one of our campaign does not have insights data on your query. You could handle that as exception in the loop.
However, I want to promote a best practice for this. In your use case, you should really use the level parameter from the ad account, unless you have hundreds campaigns under that account.
$adAccount = new AdAccount('<AD_CAMPAIGN_ID>');
$params = array(
'level' => AdsInsightsLevelValues::Campaign,
'date_preset' => InsightsPresets::LAST_7_DAYS,
);
$insights = $adAccount->getInsights(null, $params);
print_r($insights);
This way you don't even need a for loop.
And we also have a tool to guide you generate code in the Getting Started session in https://developers.facebook.com/apps/[app_id]/marketing-api/
Basically you can pick metrics and the wizard will generate a working code for you. (It is only generating Java code for now, but code pattern is the same)
I am using PhPUnit, FactoryMuffin and Faker for testing in Laravel, with a PostgreSQL db. In the previous version of FactoryMuffin (Zizaco\FactoryMuff) I could assign null values to columns both in the static factory array and when calling FactoryMuff::create.
However this no longer works - if I use the following define:
FactoryMuffin::define('MyModel', array(
'name' => 'word',
'empty' => null,
'another' => 'word'
));
when I call FactoryMuffin::create instead of passing NULL to the SQL INSERT statement it leaves the value blank, so I get:
INSERT INTO my_table ("name", "empty", "another") VALUES ('Ralph', , 'Someone');
which PGSQL doesn't allow. The same thing happens using
FactoryMuffin::create('MyModel', array('empty' => null));
Any ideas how to get round this, beyond instantiating the model and then assigning null to the field?
Since FactoryMuffin 2.1 (and 3.*) you can take advantage of the callback functionality, e.g.:
FactoryMuffin::define('MyModel', array(
'name' => 'word',
'empty' => null,
'another' => 'word'
))->setCallback(function ($object, $saved) {
$object->empty = null;
});
In FactoryMuffin 2.1 the callback is set as the third parameter of the define:
FactoryMuffin::define('MyModel', array(
'name' => 'word',
'empty' => null,
'another' => 'word'
), function ($object, $saved) {
$object->empty = null;
});
I'm not sure if the title of this question is necessarily the accurate description of what I need to do, but I'll go ahead and ask my question and see what everyone thinks...
Basically, I am receiving data from a source that I have no control over, and I need to transpose it into a suitable format for inserting into my database using CakePHP. So, here's how I'm doing it:
public function submitApp($data) {
$array = array(
'Student' => array(
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
'address' => $data['address'],
'dob' => $data['dob'],
'gender' => $data['gender']
),
'Application' => array(
'course_id' => $data['course_id'],
'question1' => $data['question1'],
'question2' => $data['question2'],
'question3' => $data['question3'],
'question4' => $data['question4'],
),
'ApplicationQualification' => $data['Qualifications']
);
// Logic to save $array goes here
}
The problem is that sometimes not all of the keys in $data will be submitted to my app but I still want my app to work with what it gets.
I know that I can wrap each key in a conditional like this:
if (!isset($data['name'])) { $data['name'] = null; }
...and then building the array, but this seems like a pretty clumsy way of doing it. Is there a more efficient way to do this?
You could use a simple ternary statement
'name' => array_key_exists('name', $data) ? $data['name'] : null
Alternatively, you can set up a default array and then merge the given values in
$defaults = [
'name' => null,
'email' => null,
// etc
];
$data = array_merge($defaults, $data);