Array of selected options in Form::select? - laravel

I have an array that contains ids of selected options in select list (html).
In Laravel it looks as:
<?=Form::select('type_work[]', $work_types, old('type_work'), ['multiple' => "multiple", 'size' => 15, 'id' => 'type_work', "class" => "selectpicker form-control"]);?>
Where second parameter is incoming data to build options, and third parameter is array of selected options (id).
Why it does not work in Laravel?
This is incoming data:
Collection {#488 ▼
#items: array:14 [▼
14 => "Роботи, пов’язані з проведенням технічної експертизи."
]
}
This is selected data:
Collection {#408 ▼
#items: array:1 [▼
14 => "Роботи, пов’язані з проведенням технічної експертизи."
]
}

The amount type_work[] you have used is considered as the ID of the field. You must specify the value of the Name as an array.
Change the code as below
{{Form::select('type_work',$work_types,null,array('multiple'=>'multiple','name'=>'type_work[]'))}}

So you code should be like
<?=Form::select('type_work[]', $work_types, null, ['multiple' => true, 'size' => 15, 'id' => 'type_work', "class" => "selectpicker form-control"]);?>
Reference
Docs

Related

Retrieving data from a countries API

I'm using an API to get a list of countries in Laravel. I have been able to isolate the countries list only but the countries now have an inner array with details of the country. I only need the name. I've tried using foreach but it didn't work.
This below is the result from the api
#items: array:2 [▼
"countries" => array:198 [▼
0 => array:4 [▼
"code" => "BT"
"name" => "Bhutan"
"geonameid" => 1252634
"depends_on" => null
]
1 => array:4 [▶]
This below is the code fetching it
$response = Http::withHeaders([
'x-rapidapi-host' => 'world-geo-data.p.rapidapi.com',
'x-rapidapi-key' => '1fa601995fmshcc1d2fcc97ee24cp1ac29djsn9b9b11cc10ec',
//'x-rapidapi-key' => config('app.rapid_api_key'),
])->get('https://world-geo-data.p.rapidapi.com/countries');
//dd($response->collect());
return $response->collect()->pluck('name')
->all();
I'm trying to get only the name value. the return is returning null
I think this is what you want:
return collect($response->collect()["countries"])->pluck('name');
The result will be like:

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)
}
}

Get all names from array and pluck them together Laravel 8

I got an array in witch are nested arrays and I want to get names from those nested arrays and display them together. I have been searching a lot and can't figure out how. I am using laravel 8.
array:2 [▼
"5dd48809b061f7a3828cb5267794ce39" => array:10 [▼
"name" => "test"
]
"7d504e7f837b2f9bb03dca5f9c8da9f5" => array:10 [▼
"name" => "test2"
]
]
You can do that in basic PHP with the array_column function.
$names = array_column($array, 'name');
The above example would output:
array(
[0] => 'test',
[1] => 'test2',
);
You can also keep the ID as key, if it exists within the nested array:
$names = array_column($array, 'name', 'id');
The above example would e.g. output:
array(
[1515] => 'test',
[2522] => 'test2',
);
There are more examples in the documentation:
https://www.php.net/manual/en/function.array-column.php

How retrieve element from collection by name?

First, I create collection from array:
$bank_center = collect(array("amount" => null, "mfo" => null, "name" => null));
Then I try to get value by key:
dd($bank_center->name);
Dump is:
Collection {#562 ▼
#items: array:3 [▼
"amount" => null
"mfo" => null
"name" => null
]
}
In your particular case, the following would just work:
$bank_center['name'];
I am not sure why you want to wrap it as an object, but if you still wish to do it, I'd recommend you take a look at Fluent.
$bank_center = new \Illuminate\Support\Fluent(array("amount" => 'test', "mfo" => 'test2', "name" => 'test3'));
dd($bank_center->name); // test3
You should use square brackets to access item from such collection:
$bank_center['name']
To retrieve element by name from collection you can use get method, it returns the item at a given key. If the key does not exist, null is returned:
$collection = collect(['name' => 'bruno', 'framework' => 'laravel']);
$value = $collection->get('name');
// bruno

How to transpose this into an array?

I am trying to transpose a Collection into an array. I'm not sure what's the method to do this. I think it's due to my lack of understanding in the eloquent operators/commands. I've been trying with map but had not made any headway.
Data
Collection {#911 ▼
#items: array:4 [▼
"HIGH" => Collection {#902 ▼
#items: array:2 [▼
0 => Finding {#680 ▶}
1 => Finding {#681 ▶}
]
}
"MEDIUM" => Collection {#903 ▶}
"LOW" => Collection {#904 ▶}
"INFO" => Collection {#905 ▶}
]
}
I would like to transpose this into an array ['HIGH' => 2, 'MEDIUM' => 1, 'LOW' => 13 ...]
I tried to apply a map but it's not giving me what i want. (tried applying the below)
... ->map (function ($risk) { return $risk[0]; });
Looking for tips on learning about these map operators and also how to transpose the Collection result above. Any help will be the most welcome!
Use toArray function to convert the collection to the array
$collection = collect(['name' => 'Desk', 'price' => 200]);
$collection->toArray();
Hope this link helps you.
https://laravel.com/docs/5.7/collections#method-toarray

Resources