0

I saw this question: Generate drop-down list input with values from DB table, but the answer was not clear.

So, how can I generate form using Illuminate/HTML and database values in Laravel?

1 Answer 1

0

It's this simple.

First, you have to pass an array with the select values from the controller:

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    $categories = Category::lists('name','id');
    return view('myformview', compact('categories'));
}

Where "Category" is the model I want to retrieve data from. Note I am passing two values to the array. The first one is the text will be displayed in the select, and the second one is the value of the option.

Let's see now the form:

{!! Form::label('category_id', 'Category:') !!}<br>
{!! Form::select(
        'category_id',
        $categories
    ) 
!!}

The first parameter of Form::select is the name of the select. The second one is the array with the options values.

In my particular case I get back this HTML code:

<select id="category_id" name="category_id">
    <option value="5">First category</option>
    <option value="6">Second category</option>
    <option value="7">Third category</option>
</select>

I hope this will be helpful for you. I tested it in Laravel 5

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.