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

ArsenMK's avatar

laravel select search

this is my db

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

I need that when I go to create.blade.php and click on select id, the length and width of this id would appear

0 likes
22 replies
Sinnbeck's avatar

Which id?

$x = YourModel::find(1);
$length = $x->length;
$width= $x->width;
1 like
Sinnbeck's avatar

@ArsenMK you select in the controller and pass it to the view (blade file)

In blade you can do

{{$x->length}} 
1 like
ArsenMK's avatar

@Sinnbeck create.blade.php

     <form  action="{{ route('productions.store') }}" method="POST">
                    @csrf
         <div>
       <div class="col" style="margin-top: 36px;">
                <select  name="" style="width: 170px" class="form-control" >
                        <optgroup   label="width">
                     @foreach($products as $product)
                         <option value="{{$products->width}}"> {{$products->width}}  </option>
                     @endforeach

                        </optgroup>
                </select>
       </div>

             <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                 <button type="submit" class="btn btn-primary">Submit</button>
             </div> 
         </div>
                </form>

ProductionController@create

  public function create()
    {
        $products = Product::all();
        $length = $products->length;
        $width= $products->width;
 return view('productions.create',compact('products'));
    }

ProductionController@store

  public function store(Request $request)
    {
        Product::create([
            'length' => $request['length'],
            'width' => $request['width'],
            'area'=> $request['width']*$request['length']
        ]);
Sinnbeck's avatar

@ArsenMK you get every single product

$products = Product::all();
return view('productions.create',compact('products'));

But your blade file just passes 1 width? Should the user choose width and length separately or together?

1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Did you mean to do this?

<select  name="product_id" style="width: 170px" class="form-control" >
foreach($products as $product)
                         <option value="{{$product->id}}"> {{$product->width}}  -  {{$product->length}}   </option>
                     @endforeach

And in store()

$product = Product::find($request->input('product_id' );
Product::create([
            'length' => $product->length,
            'width' => $product->width, 
            'area'=> $product->length * $product->width
        ]); 
1 like
Sinnbeck's avatar

Be aware that no one except you know what you are trying to build, so we can only guess your logic

ArsenMK's avatar

@Sinnbeck I want to select id and when I select id I want automatic width and length to come

ArsenMK's avatar

@Sinnbeck I need that when I select an id and click on submit, the width and length of THIS id are saved in the database in one table

ArsenMK's avatar

which of these can you tell me how to do

ArsenMK's avatar

Trying to get property 'length' of non-object

Sinnbeck's avatar

@ArsenMK can you show exactly what you have? Be aware that I changed your code a bit? And where do you get the error? When storing?

If you do then try

$product = Product::findOrFail($request->input('product_id'); 
ArsenMK's avatar

@Sinnbeck error is here

    1 public function store(Request $request)
    2 {
    3   $product = Product::find($request->input('product_id' ));
    4  Product::create([
    5      'length' => $product->length,
    6      'width' => $product->width,
    7      'area'=> $product->length * $product->width
    8
    9   ]);

error is in 5 line

Please or to participate in this conversation.