Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ArsenMK's avatar

Laravel select by id

this is myproducts table

 public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('length')->nullable();
            $table->string('width')->nullable();
            $table->timestamps();
        });
    }

this is my db

id  length  width
1   300     400
2   350     500
3   430     125
4   980     410

this is store function in my controller

public function store(Request $request)
    {
        Product::create([  
            'length' => $request['length'],
            'width' => $request['width'],
        ]);
   return redirect()->route('products.create')
            ->with('success','product created successfully.');
    }

i want that when i in create.blade.php SELECT(dropdown) id and click submit he saved in the db the length and width of this id ( from the same table ) I want the code for the backend to enter in store function

0 likes
9 replies
tykus's avatar

You want to store in the products table the same length and width as has been previously stored in that table???

ArsenMK's avatar

@tykus well practically.I just need a code that can do what i want

ArsenMK's avatar

@tykus Well, there's a lot of things in the database, I just simplified

tykus's avatar

@ArsenMK if you want a list for a dropdown, you can use pluck Builder method:

$options = Model::pluck('column', 'id');

This returns an array keyed by the ID and the value is from the given column.

If you need to make the value by concatenation of multiple columns (e.g. "length x width"), you can make an accessor , get the Model instances, and pluck the Collection

$options = Model::all()->pluck('accessor', 'id');
1 like
tykus's avatar

@ArsenMK

<select name="whatever_id">
    <option>Please select</option>
    @foreach($options as $id => $option)
        <option value="{{ $id }}">{{ $option }}</option>
    @endforeach
</select>
1 like
tykus's avatar

@ArsenMK you can, but I don't know how that makes any sense. Can you describe your actual problem that you are trying to solve?

Please or to participate in this conversation.