Using Http::post to get data from API - laravel

I tried to get data from API, when in Postman it's working fine but when i tried it in laravel it return an error
How do i get that data to laravel?
This is from my controller :
$response = Http::post('https://url.id/api/tryout/check', [
'category_id' => 0,
'platform' => 'platform',
'is_unique' => false,
'chapters' => [
'id' => 3,
'name' => "Aljabar",
'count' => 10,
],
]);
return $response;
But the code above will return an error :
Trying to access array offset on value of type int

you are passing your chapters data as array of object in your POSTMAN,
"chapters": [
{
"id": 1,
..
..
},
{
"id": 3,
..
..
}
]
then its a plain object in your Laravel HTTP request, which throwing an error because you are looping through id, name and count and not through an object containing those key which your loop expects to have
"chapters": {
"id": 3,
..
..
}
you need this part to be a list of objects
'chapters' => [
'id' => 3,
..
],
should be
'chapters' => [
[
'id' => 3,
..
]
],

You are sending 'category_id' => 0 I don’t believe that it exists in your database. You have to change it to 'category_id' => 1
And also you have to follow this answer too.

Related

How to test array contains only objects with PHPUnit?

I'm looking for solution to test an array of objects with PHPUnit in my Laravel project.
This is my haystack array:
[
[
"id" => 10,
"name" => "Ten"
],
[
"id" => 5,
"name" => "Five"
]
]
And this is the needles array:
[
[
"id" => 5,
"name" => "Five"
],
[
"id" => 10,
"name" => "Ten"
]
]
The order of objects doesn't matter, also keys of objects doesn't matter. The only matter is we have two objects and all objects has exactly same keys and exactly same values.
What is the correct solution for this?
You can do this using the assertContainsEquals method like this:
$haystack = [
[
'id' => 10,
'name' => 'Ten'
],
[
'id' => 5,
'name' => 'Five'
]
];
$needles = [
[
'name' => 'Five',
'id' => 5
],
[
'id' => 10,
'name' => 'Ten'
]
];
foreach ($needles as $needle) {
$this->assertContainsEquals($needle, $haystack);
}
You could also a create your own assert method if you intend to perform the assertion more often:
public function assertContainsEqualsAll(array $needles, array $haystack): void
{
foreach ($needles as $needle) {
$this->assertContainsEquals($needle, $haystack);
}
}
Based on #Roj Vroemen's answer I implemented this solution for exact match asserting:
public function assertArrayContainsEqualsOnly(array $needles, array $haystack, string $context = ''): void
{
foreach ($needles as $index => $needle) {
$this->assertContainsEquals(
$needle,
$haystack,
($context ? $context . ': ' : '') . 'Object not found in array.'
);
unset($haystack[$index]);
}
$this->assertEmpty(
$haystack,
($context ? $context . ': ' : '') . 'Not exact match objects in array.'
);
}

Laravel Elasticsearch JSON Mapping Issue

I'm currently using Laravel v7.2, have the babenkoivan/scout-elasticsearch-driver installed (4.2) and am using AWS Elasticsearch 7.1. I have several tables mapped in my application that are working fine but am having issues with a nested mapping that was previously working and is now broken.
I'm saving data into a table and having that table data copied into AWS Elasticsearch. I'm using MySQL 5.6 so I am using a TEXT column to store JSON data. Data in the table looks as follows:
'id' => 1,
'results' => [{"finish":1,"other_id":1,"other_data":1}]
I have my model setup with the following mapping:
protected $mapping = [
'properties' => [
'results' => [
'type' => 'nested',
'properties' => [
'finish' => [
'type' => 'integer'
],
'other_id' => [
'type' => 'integer'
],
'other_data' => [
'type' => 'integer'
]
]
],
]
];
And if it's of any use, the toSearchableArray:
public function toSearchableArray()
{
$array = [
'id' => $this->id,
'results' => $this->results
];
return $array;
}
I have no problem creating this index and it worked up until about a couple of months ago. I don't know exactly when, as it wasn't a high priority item and may have occurred around an AWS ES update but not sure why this in particular would break. I receive the following error now:
{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"object mapping for [results] tried to parse field [results] as object, but found a concrete value"}],"type":"mapper_parsing_exception","reason":"object mapping for [results] tried to parse field [results] as object, but found a concrete value"},"status":400}
I've tried also storing the data in the table as such, thinking it was breaking due to the potential array, but it was to no avail:
'id' => 1,
'results' => {"finish":1,"other_id":1,"other_data":1}
I'm at a loss for what else to try to get this working again.
EDIT: Here is the entire model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use ScoutElastic\Searchable;
class ResultsModel extends Model
{
use Searchable;
protected $indexConfigurator = \App\MyIndexConfiguratorResults::class;
protected $searchRules = [
//
];
protected $mapping = [
'properties' => [
'results' => [
'type' => 'nested',
'properties' => [
'finish' => [
'type' => 'integer'
],
'other_id' => [
'type' => 'integer'
],
'other_data' => [
'type' => 'integer'
]
]
],
]
];
public function searchableAs()
{
return 'results_index';
}
public function toSearchableArray()
{
$array = [
'id' => $this->id,
'results' => $this->results
];
return $array;
}
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'results_table';
}
Here is the \App\MyIndexConfiguratorResults::class
<?php
namespace App;
use ScoutElastic\IndexConfigurator;
use ScoutElastic\Migratable;
class MyIndexConfiguratorResults extends IndexConfigurator
{
use Migratable;
protected $name = "results_index";
/**
* #var array
*/
protected $settings = [
//
];
}
This is all that is needed to have Laravel automatically update AWS ES each time the table is updated. For the initial load, I would SSH in and run the following command to have it create the index. This, as well as elastic:migrate and any update/insert into the table produces the mapping error.
php artisan elastic:create-index results_index
Finally figured this out so will share the solution for anyone that runs into this. Turns out to be a fairly simple fix, though I'm not sure how it even worked in the first place so that part is still baffling.
I created a brand new index and update the mappings accordingly to add "id" and remove the type "nested" from the "results" piece. (Adding the "nested" type was adding two "results" to the index - one that contained all my nested data, the other just being "object".)
protected $mapping = [
'properties' => [
'id' => [
'type' => 'integer'
],
'results' => [
'properties' => [
'finish' => [
'type' => 'integer'
],
'other_id' => [
'type' => 'integer'
],
'other_data' => [
'type' => 'integer'
]
]
],
]
];
Then I simply added json_decode to the toSearchableArray() function as so:
public function toSearchableArray()
{
$array = [
'id' => $this->id,
'results' => json_decode($this->results, true)
];
return $array;
}
Voila. It successfully created the index and imported the data in a manner with which I can query the nested object.
Reading through the docs, the type field seems to have been removed. Scroll down to 7.x to see. Also, it seems you need to delete the index and re-add it in order for the new map to work according to this page.

Laravel: How can I assertJson an array

I am creating a feature test for a Seminar. Everything is working great; I am trying to update my feature test to account for the seminar dates.
Each Seminar can have one or many dates, so I am saving these values as a json field:
// migration:
...
$table->json('dates');
...
Here is what my Seminar model looks like:
// Seminar.php
protected $casts = [
'dates' => 'array',
];
When saving the seminar, I am returning a json resource:
if ($seminar->save()) {
return response()->json(new SeminarResource($seminar), 200);
...
Using Postman, my Seminar looks a like this:
...
"capacity": 100,
"dates": [
"2020-10-15",
"2020-10-16"
],
...
So far so good!
In my test, I am testing that a seminar can be created.
$http->assertStatus(201)
->assertJson([
'type' => 'seminars',
'id' => (string)$response->id,
'attributes' => [
'dates' => $response->attributes->dates, // General error: 25 column index out of range
I've tried to convert the array to a string, or json_encode the value in the resource. I don't think that's the correct way since I am already casting the value as an array in the model.
How can I assert that my dates is returning an array?
+"dates": array:2 [
0 => "2020-10-15"
1 => "2020-10-16"
]
Thank you for your suggestions!
EDIT
When I dd($response->attributes->dates); this is what I'm getting (which is correct).
array:2 [
0 => "2020-10-15"
1 => "2020-10-16"
]
What I'm not sure is how to assert an array like that. Since I'm using faker to generate the date, I don't really know (or care) what the date is, just want to assert that it is in fact an array.
I've tried something like:
'dates' => ['*'],
However, that just adds another element to the array.
EDIT 2
If I make the array a string,
'dates' => json_encode($response->attributes->dates),
I'll get an error like this:
--- Expected
+++ Actual
## ##
- 'dates' => '["2020-10-15","2020-10-16"]',
+ 'dates' =>
+ array (
+ 0 => '2020-10-15',
+ 1 => '2020-10-16',
+ ),
In my database, the values are stored like this:
["2020-10-15","2020-10-16"]
My actual test looks like this:
$http->assertStatus(201)
->assertJsonStructure([
'type', 'id', 'attributes' => [
'name', 'venue', 'dates', 'description', 'created_at', 'updated_at',
],
])
->assertJson([
'type' => 'workshops',
'id' => (string)$response->id,
'attributes' => [
'name' => $response->attributes->name,
'venue' => $response->attributes->venue,
'dates' => $response->attributes->dates,
'description' => $response->attributes->description,
'created_at' => (string)$response->attributes->created_at,
'updated_at' => (string)$response->attributes->updated_at,
],
]);
$this->assertDatabaseHas('workshops', [
'id' => $response->id,
'name' => $response->attributes->name,
'venue' => $response->attributes->venue,
'dates' => $response->attributes->dates,
'description' => $response->attributes->description,
]);

Override eloquent relationship result data

I use Laravel-Metable package in my project. This package return collection object using in key meta name and on value eloquent object.
Here you can see package data result screenshot.
How I can override result data and get this type of array data:
$meta = [
[
'id' => 1,
'key' => "Meta Name",
'value' => "Meta Value"
],
[
'id' => 2,
'key' => "Meta Name",
'value' => "Meta Value"
],
];
I will load my models meta with lazy loading:
use Metable;
protected $with = ['meta'];
You can use the collection map method for that so it should be something like this:
$result = $metaItems->map(function($meta) {
return [
'id' => $meta->id,
'key' => $meta->key,
'value' => $meta->value
];
})->values();
// then $result->toArray(); should give you the expected result

hiding an index of array laravel

I want to ask, how to hidden a name in role login.
So I have a output in laravel like this:
{
"npp":"822345",
"nama":"Handra Pratama",
"bus_pergi":1,
"bus_pulang":4,
"hotel":null,
"kamar":"K1",
"teman_kamar":[
{
"nama":"Handra Pratama"
},
{
"nama":"Louis Vernando"
},
{
"nama":"Hallo Budi"
}
]
}
I want to hide role handra (because I'm login with handra username) in teman_kamar, and if i login role louis, i want to hide louis in teman_kamar, what should i do?
Your output is in JS, so you can use a filter function in JS. But if you want to do it in PHP here is an example that I ran and it works per your case, because you always have the name that you want to hide under the first name key.
<?php
$obj = [
"npp" => "822345",
"nama" => "Handra Pratama",
"bus_pergi" => 1,
"bus_pulang" => 4,
"hotel" => null,
"kamar" => "K1",
"teman_kamar" => [
[
"nama" => "Handra Pratama"
],
[
"nama" => "Louis Vernando"
],
[
"nama" => "Hallo Budi"
]
]
];
$obj['teman_kamar'] = array_filter($obj['teman_kamar'], function($val) use ($obj) {
return $val['nama'] !== $obj['nama'];
});
print_r($obj);

Resources