scaffold-interface icon indicating copy to clipboard operation
scaffold-interface copied to clipboard

How to add multi-value field?

Open hondaman900 opened this issue 6 years ago • 6 comments

I'm trying to make one of the user input form fields a drop-down options list. I can do this (add the HTML for the

Thanks in advance for your help.

hondaman900 avatar Jan 04 '18 22:01 hondaman900

@hondaman900, thanks for submitting this issue. If I understand your issue, you can create a new entity using scaffold-interface and link it to the user entity via One-To-Many relationship, all you have to do is:

  • Go to the interface.
  • While creating the new entity, make sure to link it with user's entity selecting the username through the interface.

This will generate (CRUD) for your new entity and generate user drop-down in create.blade.php so you can select a user while creating a record into the database (entity table).

Please check the following link scaffold-interface OneToMany.

amranidev avatar Jan 05 '18 14:01 amranidev

Thanks for the quick response. That's not exactly what I was trying to achieve. I was hoping that the generated data entry form could provide my user with a drop-down list of options for some fields, rather than having them type in entries that need to be one from a pre-determined set of values.

For instance, for a user entering their address they should be able to select a drop-down list of states to enter their state, rather then type in the state letters. It's a limited preset list and this avoids typo errors. I was hoping that in the blades for edit or create that I could simply code in a form element with pre-populated list of options, and have the result of the user's choice be recorded in the database and displayed as a regular field in the index and show blades.

hondaman900 avatar Jan 05 '18 23:01 hondaman900

Update: I did get mostly success implementing a populated drop-down for a form field by replacing the blade form element with a

I did try to create an entity with a user drop-down as you suggested above and following your documentation, but the "Associate" button created provides the single option to select users but then gives a "page not found" error. Have to dig deeper into that to get it to work.

From your comments I thought that the logged-in user could create records that wouldn't be available/visible to a different user. However, switching to a different user shows all records. All users see all records. Isn't isolating content owned by separate users not the default behavior or do I need to set that up somewhere?

Thanks again for your support - much appreciated.

hondaman900 avatar Jan 06 '18 00:01 hondaman900

  1. before migrating user table to the database make sure to add ORM migration adding $table->string('state');

  2. Pass an array of data to create.blade.php through the controller.


public create()
{
     $states = ["1" => "usa", "2" => "uk", "3" => "japan"];
     return view('example.create', compact("states"));
}
  1. Add the necessary html that we desperately need to your create blade.
<form . . . >
....        
     <div class="form-group">
        <label for="state">State</label>
        <select name="state" id="state">
            @foreach($states as $key => $value)
                <option value="{{$key}}">{{$value}}</option>
            @endforeach
        </select>
      </div>
...
</form>
  1. Store the piece of the data into the database through controller's store method
    public function store(Request $request)
    {
        $user = \App\User::findorfail(Auth::user()->id);
        $user->state = $request->state;
        return redirect('example');
    }

I hope this will answer your issue!!

amranidev avatar Jan 06 '18 01:01 amranidev

@hondaman900 Hi again!!

scaffold-interface uses the great laravel/spatie-permession package that makes you eligible to give permissions to your users to display some contents to them etc...

amranidev avatar Jan 06 '18 17:01 amranidev

Hi, this is a nice package but I think that the developers have stopped working on it. Maybe you can try this package: https://github.com/Muhammadinaam/speed-admin. It supports Laravel 8+.

Muhammadinaam avatar Jul 09 '21 01:07 Muhammadinaam