D
D
Dan2022-01-14 21:13:01
JavaScript
Dan, 2022-01-14 21:13:01

How to get data from select in laravel?

Good afternoon / evening, please tell me how to save data with a select loop.

controller

namespace App\Http\Controllers\Front;

use Illuminate\Http\Request;
use App\Booking;
use App\Marka;
use App\Models;


class MarkaController extends Controller
{
    public function index()
    {
        $markasi = Marka::all();
    if ($this->frontThemeSettings->front_theme == 'theme-2') {
            return view('front-new.booking', ['markasi' => $markasi]);
        }
    }

    public function selectModels(Request $request){
        if($request->ajax()){
            $models = Models::where('id_markas',$request->id_markas)->get()->pluck("name","id");
            $data = view('selectmodels',['models' => $models])->render();
            return response()->json(['options'=>$data]);
        }
    }
}


booking.blade.php

<div class="col-md-6">
<div class="form-group mb-30 rounded userDetailForm">
    <select name="markaauto" id="markaauto" class="form-control form-control f-13 select2">
      <option disabled selected>@lang('front.registration.markaauto')</option>
      @foreach($markasi as $marka)
        <option value="{{ $marka->id }}">{{ $marka->name }}</option>
      @endforeach
    </select>
  </div>
</div>	
<div class="col-md-6">
  <div class="form-group mb-30 rounded userDetailForm">
    <select name="marka" id="marka" class="form-control form-control f-13 select2" >
      <option disabled selected>@lang('front.registration.marka')</option>
    </select>
  </div>
</div>


selectmodels.blode.php

<option>@lang('front.registration.marka')</option>
@if(!empty($models))
    @foreach($models as $key => $value)
        <option value="{{ $key }}">{{ $value }}</option>
    @endforeach
@endif


Save now like this

public function saveBooking(StoreFrontBooking $request)
{
  if ($this->user) {
    $user = $this->user;

    $address = Address::firstOrNew(['user_id' => $user->id]);
    $address->marka = $request->marka;
    $address->markaauto = $request->markaauto;
    $address->vin = $request->vin;
    $address->yearauto = $request->yearauto;
    $address->save();
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question