Viewed   94 times

I want to display an array of values as options inside a select using the laravel-nova syntax. I managed to get the options rendered inside the select but the values of these options are like

<option value="2"></option>
<option value="1"></option>
<option value="0"></option>

what I want is the text as an value.

this is what I got so far:


public function selectOptions()
    $urls = DB::table('subpages');
    $slugs = $urls->pluck('slug');

    return $slugs;

What am I doing wrong?



Make sure to get() the subpages as IlluminateSupportCollection and mapWithKeys() to reformat the results. Use toArray() to provide the format Nova assumes:

private function selectOptions(): array
    $subpages = DB::table('subpages')->get();
    return $subpages->mapWithKeys(function ($subpage) {
        return [$subpage->slug => $subpage->slug];

This is how the returned result should look like:

    'my-article-1' => 'my-article-1',
    'my-article-2' => 'my-article-2',
    'my-article-3' => 'my-article-3',
Thursday, September 1, 2022

Recommended approach if you put here only methods (not classes):

  1. Create file anywhere you want
  2. In composer.json make sure you add this file to files key inside autoload like this:

    "autoload": {
        // here other autoload things
        "files": ["app/Helpers/AnythingHelper.php"]
  3. Run composerdump-autoload`

For classes obviously you should use standard PSR-4 autoloading

Sunday, October 23, 2022

you cannot add many values in an option. instead you can use datasets (html5), like:

<select id="myselect">
 <option data-this="foo" data-that="bar">

the javascript to read these values is:

var d = document.getElementById("myselect");
var _this = d.options[d.selectedIndex].dataset["this"];
var _that = d.options[d.selectedIndex].dataset["that"];

if you dont want to mess with datasets, you can store a JSON object:

<option value='<?php echo json_encode(array("foo"=>1,"bar"=>2)); ?>'>

and extract the data like:

var d = document.getElementById("myselect");
var option_value = JSON.parse( d.options[d.selectedIndex].value );

you choose


change this:

<select onChange="display(<?=$manufacturers_id?>,<?=$modelID?>,this);">

function display(manufacturers_id,modelid,obj) {
   ... json way
   var option_value = JSON.parse(obj.options[obj.selectedIndex].value);
   // = 1;   

   ... dataset way
   var _this = obj.options[obj.selectedIndex].dataset["this"];   
   // _this will have the "foo" value
Monday, November 14, 2022

It depends on the driver used between php and mysql.

Check which one of them is used by checking pdo_mysql section of the output of

php -i

your output should be similar to


PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.12-dev - 20150407 - $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $

The native driver return integers as integers, but the other return them as strings.

So the solution is to remove the old driver and install the native one.

or to use $casts into your model.

    protected $casts = [
    'status' => 'integer',
Sunday, August 14, 2022

After trying a lot, I realized this was an issue due to SELinux in force in my system. SELinux prevented apache from writing to required files.

$ /usr/sbin/getenforce

I disabled SELinux to confirm this doubt and it worked properly once SELinux was disabled. So I just needed to set proper permission for my project, so SELinux can allow apache to write to necessary files.

$sudo chcon -t httpd_sys_rw_content_t /path/to/my/laravel/project/dir -R

I hope this will help to those who are facing same issue.

Wednesday, August 24, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :