0

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' : '',
        ],
    ]);
1
  • Generate select manually. Commented Jun 18, 2018 at 9:27

2 Answers 2

1

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() }}
Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

This is only half of a full answer. He is also confused on how to display all the data. I don't think you want a select that can select between name/title of one object. It doesn't make sense. If I'm correct you need more Form elements than just one.
@Kerel I hope you read this line from question "But in this way It'll display only title How can I display Name and title some thing like this"
Yes I did, and "Form::select('product',$product, null, ..." is not gonna work for multiple fields. You need more of these.
Updated for a change

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.