Farah Aidina
Farah Aidina

Reputation: 115

Get name and value from option select in laravel

I'm using foreach to show the option from database, which has value like this:

<select name="location[]" data-placeholder="Search Location" multiple class="chosen-select">
   <?php foreach ($location as $key): ?>
     <option value=""></option>
     <option value="{{$key->id}}">{{$key->name}}</option>
   <?php endforeach; ?>
</select>

I want to pass both the {{$key->name}} and {{$key->id}} to my controller to get a response like this {'name':'city', 'id': '2'}.

For now I'm only using the common way to pass the {{$key->id}} into controller using this method:

$loc = $request->location;
$i = 0;
foreach ($loc as $value) {

  if ($value == true) {
   $date_opt[] = [
     "id" => $value,
   ];
  }
  $i++;
}
return $date_opt;

But have no idea how to pass the {{$key->name}}.

Should I put the {{$key->name}} in another table or is there any possible way to do it?

Upvotes: 1

Views: 3436

Answers (3)

nikistag
nikistag

Reputation: 694

Send both data as value:

<option value="{{$key->id .'-'.$key->name }}">{{$key->name}}</option>

In controller do this

$location = explode("-", $request->input('location'));

Then $location[0] - holds ID, and $location[1] holds NAME

Upvotes: 0

opinatus
opinatus

Reputation: 106

Its probably best in general practise not to send through this information and then save it based off the name provided through the select, but rather to just send the id and then have relationships which can pull through the name or/and store the name directly into that table by getting the relationship on the POST method and saving the name directly from the Database, if that makes sense.

But if you for some reason need to store the name in the request, here's a couple of ways it could be done.

You could look at using arrays to get somewhere but I'm not sure if that is what you're looking for

<option value="location[{{ $key->id }}][{{ $key->name }}]" />

I'd have to check but I'm not sure if you need to if you'll be able to do something like:

<option value="[{{ $key->id }}, {{ $key->name }}]" />

But not sure that'll come out in a very useable manner into your controller, another way to do this

<option value="{{ $key->id }}:{{ $key->name }}" />

Then in your controller exploding the value by the :, which would probably be easier in that instance.

$parts = explode(':', $value);
$id = $parts[0];
$name = $parts[1];

Upvotes: 2

Majid Mushtaq
Majid Mushtaq

Reputation: 319

Place following in template :

<select name="location[]" data-placeholder="Search Location" multiple class="chosen-select">
<?php foreach ($location as $key): ?>
   <option value=""></option>
   <option value="{{$key->name}},{{$key->id}}">{{$key->name}}</option>
<?php endforeach; ?>
</select>

Place Following code in controller :

$loc = $request->location;
$i = 0;
foreach ($loc as $value) {

   if ($value == true) {
     $data_arr = explode(',', $value);
     $date_opt[] = [
       "name" => $data_arr[0]
       "id" => $data_arr[1],
      ];
    }
   $i++;
  }
return $date_opt;

There are other ways to achieve but as per your provided code this is simple solution.

Upvotes: 0

Related Questions