Home > other >  how to get values from a table by providing its id as foreign key in another table
how to get values from a table by providing its id as foreign key in another table

Time:07-08

I have two tables, first 'customers' and second 'linked'. The following are their columns respectively.

id | customer_name | contact_person_name | contact_person_no

id | fk_customer_id | item_image

Now, in my views there is a select box where user will select a customer name and I want that customers item_image (from second table) to be displayed below. Any help would be appriciated.

This code is from views

<select type="search" name="customer_id"  name="customer_id" 
   value="{{ old('customer_id') }}" required><option 
   selected>Select Customer to link Item</option>
      @foreach ($customers as $customer)
       <option value="{{ $customer->id }}">{{ $customer- 
       >customer_name }}</option>
      @endforeach
</select>

CodePudding user response:

in my views there is a select box where user will select a customer name and I want that customers item_image

There may be several customers with the same name (unless you declared customer_name unique, even then I wouldn't count on that not changing in the future). While you display customer_name, be sure to include customers.id as the value. Search by the id.

Then it's a simple join:

select linked.item_image
from linked
join customers on customers.id = linked.fk_customer_id
where customers.id = ?
  • Related