Reputation: 159
I store my array in config/product.php
return [
[
'id' => 1,
'title' => 'test1',
'name' => 'name1'
],
[
'id' => 2,
'title' => 'test2',
'name' => 'name2'
],
[
'id' => 3,
'title' => "test3",
'name' => 'name3'
]
];
In My controller I use pluck to show my title
$product = collect(config('products'))->pluck('title','id');
$data['product'] = $product;
$data['store'] = $this->store;
return view($this->route.'.create',$data);
in my view
{!!
Form::select('product',$product, null, [
'placeholder' => 'Select Product',
'class' => [
'form-control',
$errors->has('product') ? 'is-invalid' : '',
],
]);
!!}
But in this way It'll display only title How can I display Name and title some thing like this
Form::select('product',$product->title.$product, null, [
'placeholder' => 'Select Product',
'class' => [
'form-control',
$errors->has('product') ? 'is-invalid' : '',
],
]);
Upvotes: 0
Views: 919
Reputation: 732
Firstly, change the pluck to entail the name as well.
$product = collect(config('products'))->pluck('title', 'name', 'id');
Or retrieve the whole model if it makes more sense.
Then, for every element on your form, you need to make different selects.
Form::select('product',$product->title, null, [
'placeholder' => 'Select Product Title',
'class' => [
'form-control',
$errors->has('product') ? 'is-invalid' : '',
],
]);
So you need to have this code snippet for
$product->title and $product->name.
You could also use model binding: https://laravel.com/docs/4.2/html#form-model-binding
Don't forget to read this important part when using model binding.
Now, when you generate a form element, like a text input, the model's value matching the field's name will automatically be set as the field value. So, for example, for a text input named email, the user model's email attribute would be set as the value. However, there's more! If there is an item in the Session flash data matching the input name, that will take precedence over the model's value.
So you will need to open the model first.
echo Form::model($user, array('route' => array('user.update', $user->id)))
Then you need to make the correct select fields in this form as well. In your example it would be:
echo Form::text('title');
echo Form::text('name');
Full example:
{{ Form::model($product, ['route' => ['product.update', $product->id]]); }}
{{ Form::text('title'); }}
{{ Form::text('name'); }}
{{ Form::close() }}
Upvotes: 1
Reputation: 2328
In your controller get the whole array of products.
$products = collect(config('products'));
In your blade
<div class="form-group">
{!! Form::Label('Product', 'Products:') !!}
<select class="form-control" name="item_id">
@foreach($products as $product)
<option value="{{ $product['id'] }}">{{ $product['name']." ".$product['title'] }}</option>
@endforeach
</select>
Try this
Upvotes: 0