Saturday, March 18, 2017

How to insert id to database from text input

Hi I've been searching how to do this for over 2 hours so I'll ask here.

I have an eventlineitems table with product_id. On the create form I want to type the product name and when the form submits the controller sets the product_id for the product with that name.

EventLineItems

id
event_id
product_id

Products

id
name

EventLineItemsController

public function store(Request $request)
{
  $this->validate(request(), [

    'event_id' => 'required',
    'product_id' => 'required',
    'quantity' => 'required'

  ]);

  $lineItem = new EventLineItem;

  $lineItem->event_id = $request->input('event_id');
  $lineItem->product_id = $request->input('product_id');
  $lineItem->quantity = $request->input('quantity','1');

  $lineItem->save();

  return back()->with('status','Product Added');

}    

view.blade

<input type="text" name="product_id" id="product_id" class="typeahead form-control" placeholder="Enter a product ID" required>



via DevJustin

Advertisement