Skip to content
Advertisement

Laravel : How to Create Dropdown to Select FOREIGN KEY from Other Table?

i want to ask you how to select FOREIGN KEY (from “doa_id in “doas” table) when user create a new data (to the “notes table) in a form. This is my code :

NoteController.php

JavaScript

2021_05_28_020438_create_notes_table.php migration

JavaScript

and this is my dropdown html :

JavaScript

enter image description here

In Dropdown option, I want to show “doa_name” based on it’s “doa_id”

Thank you 🙂

Advertisement

Answer

You can put the logic in the NoteController and the view. For example, to get a collection of all options for the foreign key, pass the collection to the view.

JavaScript

Then on the view, you can perform a foreach loop using the select HTML tag.

JavaScript

After this, it’s just using the input in your store() method.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement