Hourlee's avatar

Laravel show customer_name by using customer_id

I am using laravel and vue js. I have two tables: customers and projects. I have a form - a dropdown form which user can select a customer_name. However, in the projects table, I want to store customer_id from the form selected by the user. I want to use id in customers table to show customer_name in the form. How this can be done?

Thanks,

0 likes
3 replies
Cronix's avatar

Are you talking about a <select> element? If so, you'd usually just do

<select name="customer_id">
    @foreach ($customers as $customer)
        <option value="{{ $customer->id }}">{{ $customer->name }}</option>
    @endforeach 
</select>

The value is what gets submitted, so make that the id's. The user sees the $customer->name in the dropdown.

1 like
burlresearch's avatar
Level 40

Assuming you are submitting formdata from your POST as payload. Then your select logic might be like:

<select class="form-control" id="customer_id" v-model="formdata.customer_id">
    <option v-for="customer in customers" :value="customer.id" v-text="customer.name"/>
</select>

Assuming you have an array of JS customers objects being set from props, or somehow.

Please or to participate in this conversation.