0

I have a multiple select box.

Multiple select box

This is my blade.

                                <div class="form-group">
                                    <label for="service_id">Services *</label>
                                    <select class="select2" multiple="multiple" name="service_id[]" required
                                        data-placeholder="Select Services" style="width: 100%;">
                                        @foreach($services as $service)
                                        <option value="{{$service->services_id}}">{{$service->services_name}}
                                        </option>

                                        @endforeach

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

As you can see i only send {{$service->services_id}} for value i want to send {{$service->services_price}} as well and save to the database like JSON format.

It should be save to the table like this '{"1":"5000", "2":3000, "2":6666}' in database.

How can i do that ?

I dont think it will help you but this is my Controller

public function store(Request $req)
{
    //
    // Form validation (Zorunlu Alanlar)
    $this->validate(request(), [
        
        'customer_id' => 'required',
        'service_id' => 'required',
        'offer_score' => 'required',
        'tag_id' => 'required',
    



 


       
    ]);     



    $offers = new Offers;
    $offers->service_id           = implode(',', $req->service_id);
    $offers->offer_status         = $req->offer_status;
    $offers->customer_id          = $req->customer_id;
    $offers->offer_score          = $req->offer_score;
    $offers->tag_id               = $req->tag_id;
    $offers->offer_discount       = $req->offer_discount;
    $offers->offer_reminder_date  = $req->offer_reminder_date;
    $offers->user_company_id      = $req->user_company_id;
    $offers->user_id              = $req->user_id;
    $customers2 = Customers::all();

  

         $offers->updateTimestamps();
        $offers->save();





    return redirect()->back()->with('success', true);
}
Emre Kd
  • 27
  • 5

1 Answers1

0

You may also echo the results of any PHP function. In fact, you can put any PHP code you wish inside of a Blade echo statement

  1. Join the id and price in the template

    <option value="{{join('|', [$service->services_id, $service->services_price])}}">{{$service->services_name}}
    

    so, the value you send to the server may look like this "32|1500;33|2000;34|3000;35|3500;"

  2. Split the string in your controller

    $sample = "32|1500;33|2000;34|3000;35|3500;";
    
    $pair = array_filter(explode(";", trim($string, ";")), function($s){return strlen($s) > 0;});
    $idPrice = [];
    foreach($pair as $row) {
        list($key, $value) = explode("|", $row);
        $idPrice[$key] = $value;
    }
    
    //var_dump(json_encode($idPrice));
    //string(49) "{"32":"1500","33":"2000","34":"3000","35":"3500"}" 
    
Jun Pan
  • 362
  • 1
  • 11