4
Good evening, I have a project in Laravel and I’m having the following difficulty:
I have a controller that retrieves data from my database, has a variable that receives string and this string converts to array
public function edit($id)
{
$dados=Treinodet::find($id);
$obj= Treino::find($dados->treino_id);
$string = $dados->especificidade;
$exer = explode(',', $string);
$exercs= DB::select("select *from lista where id != 0 and idtipo in(8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31)");
return view('treino.exercad',compact('obj','exercs','dados','exer'));
}
play this dice to the view compare if you have any string data that matches the list if it has I give the Selected up to there everything right works 100%. But when I change any other data in this form this select always inserts the next item in the list without me doing anything.
<div class="form-group col-md-8">
<label>Exercicio</label>
<select name="especificidade[]" multiple="multiple" class="form-control select-search">
{{$i=0}}
@foreach($exercs as $item)
<option value="{{ $item->descricao }}" @if(isset($exer[$i]) == $item->descricao) selected @else @endif>{{ $item->descricao }}</option>
{{$i++}}
@endforeach
</select>
</div>
Saves the data
public function exercad(request $request)
{
$param = $request->all();
$obj= Treinodet::find($param['id']);
if(empty($obj)){
$exer = $param['especificidade'];
$stringArrayF="";
foreach ($exer as $stringArray)
{
$stringArrayF = $stringArray.', '.$stringArrayF;
}
$param['especificidade']= $stringArrayF;
$obj = Treinodet::create($param);
}
else{
$exer = $param['especificidade'];
$stringArrayF="";
foreach ($exer as $stringArray)
{
$stringArrayF = $stringArray.', '.$stringArrayF;
}
$param['especificidade']= $stringArrayF;
$obj->update($param);
}
return redirect()->back()->with('success', 'Cadastro Criado com sucesso');
}
how do I make it not happen?
I will test and answer you, I thank you for the moral
– Tulio Vieira
tested here and continues with the same problem
– Tulio Vieira
Show me the whole Controller. In order to analyze.
– douglas pjuizfora
ta ai o controller todo Douglas
– Tulio Vieira
@Tuliovieira, this controller you have placed is the one that returns to the view. I want the Controller that saves the data in the database. Because this is the part where you’re making the right mistake ? Or I got it wrong ?
– douglas pjuizfora
is here the function that saves the data @Douglas pjuizfora
– Tulio Vieira
@Tulip tree, I put a similar case of multiple selects. But I really have no idea what’s going on.
– douglas pjuizfora