[ad_1]
I’m a bit new to a project Laravel and i’m currently block on an error since hours.
I’m trying to make a select multiple, it’s working when the select is unique but when i try to have the possibility to select multiple option, it doesn’t work.
Here the view :
<label for="operation_id" class="text-gray-700 inline-block">Opération</label>
<select class="focus:outline-none outline-none w-full px-3 py-2 border rounded {{ $errors->has('operation_id') ? 'border-red-400' : '' }} select-checkbox" name="operation_id[]" id="operation_id" multiple>
@foreach ($operations as $operation)
@if (old('operation_id', $offer->operation_id ?? '') == $operation->id)
<option value="{{ $operation->id }}" selected {{ in_array($operation->id, old('operation_id') ?: []) }}>{{ $operation->translation->name }}</option>
@else
<option value="{{ $operation->id }}">{{ $operation->translation->name }}</option>
@endif
@endforeach
</select>
@if ($errors->has('operation_id'))
<div class="invalid-feedback">
<strong>{{ $errors->first('operation_id') }}</strong>
</div>
@endif
Then the controller :
class OfferController extends Controller
{
use UploadTrait;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
return view('dashboard.offers.index');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$doctors = Doctor::get();
$operations = Operation::with([
'translation' => function($q) {
return $q->where('language_code', app()->getLocale());
}
])
->get();
$hotels = Hotel::get();
$currencies = Currency::get();
return view('dashboard.offers.create', compact( 'doctors', 'operations', 'hotels', 'currencies'));
}
/**
* Show the form for editing the specified resource.
*
* @param Offer $offer
* @return \Illuminate\Http\Response
*/
public function edit(Offer $offer)
{
$language = \App\Models\Language::where('language', request()->input('lang', env('APP_LANG', 'fr_FR')))->firstOrFail();
$offer->load(['translations']);
$doctors = Doctor::get();
$operations = Operation::with([
'translation' => function ($q) {
return $q->where('language_code', app()->getLocale());
}
])
->get();
$hotels = Hotel::get();
$currencies = Currency::get();
return view('dashboard.offers.edit', compact('offer', 'doctors', 'operations', 'hotels', 'currencies', 'language'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(OfferRequest $request)
{
$offer = Offer::create($request->only('price', 'hotel_included', 'recommended_stay', 'hospitalisation', 'visible', 'operation_id', 'doctor_id', 'hotel_id', 'hospitalisation', 'price_fixed', 'currency', 'promote', 'first_picture'));
$offer->translations()->create([
'language_code' => env('APP_LOCAL', app()->getLocale()),
'title' => $request->title,
'features' => $request->features,
]);
$operationId = collect($request->input('operation_id'))->keys();
$offer->operations()->attach($operationId);
return redirect()->route('admin.offers.index')->with('success', 'Une nouvelle offre a été enregistrée');
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\OfferRequest $request
* @param OfferTranslation $offerTranslation
* @return \Illuminate\Http\RedirectResponse
*/
public function update(OfferRequest $request, Offer $offer)
{
$offer->update($request->only('price', 'hotel_included', 'recommended_stay', 'hospitalisation', 'visible', 'operation_id', 'doctor_id', 'hotel_id', 'hospitalisation', 'price_fixed', 'currency', 'promote', 'first_picture'));
$language = \App\Models\Language::where('language', request()->input('lang', env('APP_LANG', 'fr_FR')))->firstOrFail();
$offer->translations()->updateOrCreate(
['offer_id' => $offer->id, 'language_code' => $language->language],
$request->only('title', 'features')
);
$operationId = collect($request->input('operation_id'))->keys();
$offer->operations()->sync($operationId);
return redirect()->route('admin.offers.index')->with('success', "L'offre a été mise à jour");
}
When i try to update, i get this error :
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ‘[“1”]’ for column ‘operation_id’ at row 1 (SQL: update offers
set operation_id
= [“1”], offers
.updated_at
= 2022-05-30 14:56:42 where id
= 1)
I don’t know from where come the error. Thank in advance of ur help
[ad_2]