mardi 22 décembre 2015

Populate dropdown list from database with Laravel returns id instead of value

I'm appalled by this, though it should be a brush-up for Laravel experts. I'm using this code to populate a dropdown select box, which populates it very well, but when I select a value from the dropdown select box instead of giving me the value it gives the id, where do I need to improve?

This is the code used in populating the select box dropdown:

public static function ReturnComanyPerUser()
     {
        return static::where('user_id', '=', Auth::user()->id)->orderBy('companyname')->lists('companyname', 'id');
     }

This is the binding to the dropdown:

public function create()
    {
        return view('product.create')->with('companyforproduct', Company::ReturnComanyPerUser());
    }

This is the blade page where the dropdown is:

{!! Form::open(array('url' => 'created-products', 'method' => 'post')) !!}
{!! Form::token() !!}

    <p>{!! Form::select('companyname', $companyforproduct) !!} </p>
    <p>{!! Form::text('productname', Input::old('productname')) !!}</p>
    <p>{!! Form::submit('CREATE PRODUCT') !!}</p>

{!! Form::close() !!}

How do I get the value not id? When I select from dropdown it gives me the id of the value. I get a display like this:

Company name  product name
        2               fish



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire