hiding an index of array laravel - 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);

Related

Using Http::post to get data from API

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.

How I can insert array data in laravel?

I want to insert data by seeder:
public function run()
{
$projects = [
[
"name" => "project-one",
"images" => ["image11.jpg", "image12.jpg", "image13.jpg"],
],
[
"name" => "project-two",
"images" => ["image21.jpg", "image22.jpg", "image23.jpg"],
],
];
Project::insert($projects);
}
images column is JSON, I get an array error on images insertion. How I can insert them?
insert() method can not use any casts of the model, it will be inserted as raw. So you just need to convert the array to a json using json_encode() function.
public function run()
{
$projects = [
[
"name" => "project-one",
"images" => json_encode(["image11.jpg", "image12.jpg", "image13.jpg"]),
],
[
"name" => "project-two",
"images" => json_encode(["image21.jpg", "image22.jpg", "image23.jpg"]),
],
];
Project::insert($projects);
}

The $bucket 'default' field must be less than the lowest boundary or greater than or equal to the highest boundary

What does this bug refer to? I have pasted the code below. Kindly have a look. Can anyone let me know what's wrong with the boundary value here? Thanks in advance
db.match_list.aggregate(
[
{
$bucket: {
groupBy: "$competition_id",
boundaries: ["9dn1m1gh41emoep","9dn1m1ghew6moep", "d23xmvkh4g8qg8n","gy0or5jhj6qwzv3"],
default: "Other",
output: {
"data" : {
$push: {
"season_id": "$season_id",
"status_id": "$status_id",
"venue_id": "$venue_id",
"referee_id": "$referee_id",
"neutral":"$neutral",
"note": "$note",
"home_scores":"$home_scores",
"away_scores": "$away_scores",
}
}
}
}
},
{
$sort : { competition_id : 1 }
},
])
mongodb laravel query using raw. Not sure what's going wrong here.new_array vale also has been mentioned
$contents = $query->orderby('competition_id')->pluck('competition_id')->toArray();
$contents = array_unique($contents);
$new_array = array_values($contents);
$data = $query->raw(function ($collection) use ($new_array) {
return $collection->aggregate([
[
'$bucket' => [
'groupBy' => '$competition_id',
'boundaries' => $new_array,
'default' => 'zzzzzzzzzzzzzzzzzzzzzzzzz',
'output' => [
"data" => [
'$push' => [
"id" => '$id',
"season_id" => '$season_id',
"status_id" => '$status_id',
"venue_id" => '$venue_id',
"referee_id" => '$referee_id',
"neutral" => '$neutral',
"note" => '$note',
"home_scores" => '$home_scores',
]
]
]
]
]
]);
});
You have entered Other in the default parameter of the $bucket parameter which
is in between the min-max boundaries you have provided.
I would suggest you try a value of the greater or lesser string than to that provided in the
boundaries array or better enter a different datatype value such as int of 1.
db.match_list.aggregate( [
{
$bucket: {
groupBy: "$competition_id",
boundaries: ["9dn1m1gh41emoep","9dn1m1ghew6moep", "d23xmvkh4g8qg8n","gy0or5jhj6qwzv3"],
default: 1, // Or `zzzzzzzzzzzzzzzzzzzzzzzzz` (any greater or lesser value than provided in `boundaries`
output: {
"data" :
{
$push: {
"season_id": "$season_id",
"status_id": "$status_id",
"venue_id": "$venue_id",
"referee_id": "$referee_id",
"neutral":"$neutral",
"note": "$note",
"home_scores":"$home_scores",
"away_scores": "$away_scores",
}
}
}
}
},
{ $sort : { competition_id : 1 } },
] )

Laravel attach on associative key

I have a ManyToMany relationship setup but need to specify a value for the key in my array to attach.
$data = request()->validate([
'homework_title' => '', // string
'homework_description' => '', // string
'group_id' => '', // array
]);
$homework = request()->user()->homeworks()->create([
'homework_title' => $data['homework_title'],
'homework_description' => $data['homework_description'],
]);
$homework->groups()->attach($data['group_id'][key]);
group_id is an array and looks like the following:
[
{
"group_id": 1,
"group_name": "8x/En2"
},
{
"group_id": 2,
"group_name": "9x/En3"
}
]
How do I specify to attach only the group_id in array?
Got it to work, with:
Arr::pluck()
First time round I forgot to import the class (stupid me)!
use Illuminate\Support\Arr;
finally looking like the following:
$homework->groups()->attach(Arr::pluck($data['group_id'], 'group_id'));

Incomplete links generated in hal (zend-expressive-hal + zend-router)

My routes def:
'router' => [
'routes' => [
'TimeTable' => [
'type' => 'Literal',
'options' => [
// Change this to something specific to your module
'route' => '/tt',
],
'may_terminate' => false,
'child_routes' => [
'API' => [
'type' => 'Literal',
'options' => [
// Change this to something specific to your module
'route' => '/api',
],
'may_terminate' => false,
'child_routes' => [
'lines' => [
'type' => 'Literal',
'options' => [
// Change this to something specific to your module
'route' => '/lines',
'defaults' => [
'controller' => Controller\LineRestApiController::class,
],
],
'may_terminate' => true,
],
],
],
],
],
],
Medatada map def:
MetadataMap::class => [
[
'__class__' => RouteBasedCollectionMetadata::class,
'collection_class' => LineCollection::class,
'collection_relation' => 'lines',
'route' => 'TimeTable/API/Lines',
],
]
Generated result:
{
"_total_items": 78,
"_page": 1,
"_page_count": 4,
"_links": {
"self": {
"href": "http://xxx.xxx.xx"
},
"next": {
"href": "http://xxx.xxx.xx"
},
"last": {
"href": "http://xxx.xxx.xx"
}
},
"_embedded": {
"lines": [.....] }}
All links are generated with incomplete href , there is only domain part, route part is stripped ..
Expected result is something like this:
"href" : "http://xxx.xxx.xx/xxx/tt/api/lines....."
Im doing something wrong, I have no idea where to start ..
Thanks everyone for giving me some ideas
Simplified Controller code:
$psr7request = Psr7ServerRequest::fromZend($this->getRequest());
$list = this->entityManager->getRepository(Line::class)->getValidLinesCollection();
$resource = $this->resourceGenerator->fromObject($list, $psr7request);
echo Psr7Response::toZend($this->responseFactory->createResponse($psr7request, $resource))->getBody();
exit;
PS: Im not using full zend-expressive just zend-framework..
Sorry i forgot that i had done in last week :(
It must be done some custom implementation for UrlGeneratorInterface to successful integrate zend-expressive-hal to zend framework (original class ExpressiveUrlGenerator uses Expressive\Helper\ServerUrlHelper & UrlHelper, the part of Expressive)
So i used Zend\View\Helper\ServerUrl & Url to done it.
I have small typo in code. The final class is here:
use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Hal\LinkGenerator\UrlGeneratorInterface;
use Zend\View\Helper\ServerUrl as ServerUrlHelper;
use Zend\View\Helper\Url as UrlHelper;
class HalUrlGenerator implements UrlGeneratorInterface
{
/**
* #var null|ServerUrlHelper
*/
private $serverUrlHelper;
/**
* #var UrlHelper
*/
private $urlHelper;
public function __construct(UrlHelper $urlHelper, ServerUrlHelper $serverUrlHelper = null)
{
$this->urlHelper = $urlHelper;
$this->serverUrlHelper = $serverUrlHelper;
}
public function generate(
ServerRequestInterface $request,
string $routeName,
array $routeParams = [],
array $queryParams = []
) : string {
$urlHelper = $this->urlHelper;
$path = $urlHelper($routeName, $routeParams, ['query'=> $queryParams]);
if (! $this->serverUrlHelper) {
return $path;
}
$serverUrlHelper = $this->serverUrlHelper;
return $serverUrlHelper($path);
}
}
I hope the code can help somebody.

Resources