3

First Laravel Project.

I want to make a form drop-down list what is populated from mysql database. I found this on the documentation:

Form::select('size', array('L' => 'Large', 'S' => 'Small'))

and I tried this:

{{Form::select('size', array(
                @foreach ($suppliers as $supplier)
                $supplier->id => $supplier->name
                @endforeach
))
}}

But I got syntax error:

ErrorException in e34a9587ee23853b6d4c489cc0ed13515fad9c06.php line 23: Parse error: syntax error, unexpected '<', expecting ')' (View: /var/www/html/project/laravel/leltar/resources/views/invoice.blade.php)

What did I wrong?

2 Answers 2

3

Pluck returns a collection from laravel version 5.3 and that answer is wrong, it wont show the select box right!

Here is the right solution!

$suppliers = Supplier::pluck('name', 'id')->toArray();

and in view call it like this:

{!! Form::select('supplier', $suppliers, null, ['class' => 'form-control']) !!}

update for Laravel 5.5

Just tested this and in Laravel 5.5 pluck() works without appending toArray() on it like this:

$tags = Tag::pluck('name', 'id');

or for this question;

$tags = Supplier::pluck('name', 'id');
Sign up to request clarification or add additional context in comments.

Comments

2

Try the pluck method:

Form::select('size', $suppliers->pluck('name', 'id')->all())

4 Comments

I got an another error message: "Call to a member function pluck() on array (View: /var/www/html/project/laravel/leltar/resources/views/invoice.blade.php)"
how do you send $suppliers from controller?
$suppliers = DB::select('select * from supplier'); return view('invoice',['suppliers'=>$suppliers]);
$suppliers = SupplierModel::all(); try it using the model instead of a raw query

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.