Select Laravel Entering Next Record Alone

Asked

Viewed 299 times

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?

2 answers

3

The interesting thing would be to post your entire controller. To be able to analyze how it is working.

<div class="form-group col-md-8">
<label>Exercicio</label>
<select name="especificidade[]" multiple="multiple" class="form-control select-search">
    @foreach($exercs as $i => $item)
        <option value="{{ $item->descricao }}" {{ (isset($exer[$i]) == $item->descricao) ? 'selected': '' }}>{{ $item->descricao }}</option>
    @endforeach
</select>

See if this will solve your problem.

Dude, I have no idea what might be going on with your code. I’m going to put an example below as I do with multiple selects.

My controller:

public function adicionarAdicional(Request $request, Adicional $adi)
{

$auth = Session::get('auth');

if (!$auth) {
    return Redirect::to('/login')->withErrors(['Acesso negado.']);
} else {

    $request->all();

    $adicionais = $request->dt_adicional;

    $this->validate($request, [
        'dt_adicional.*' => [new Data]
    ]);

    $id_servidor = Session::get('id_servidor');

    foreach ($adicionais as $i => $adicional) {

        $dt_adicional = \DateTime::createFromFormat('d/m/Y', $adicional)->format('Y-m-d');

        $adici = $adi::create(array(
            'id_servidor' => $id_servidor,
            'dt_adicional' => $dt_adicional,
        ));
    }
}
}

And below my view:

@foreach ($datas as $i=>$data)
    <div class="col-md-3">
        <div class="form-check">
            <input class="form-check-input" type="checkbox" value="{{$data}}" id="dt_adicional{{$i}}" name="dt_adicional[]">
            <label class="form-check-label" for="dt_adicional{{$i}}">
                {{$data}}
            </label>
        </div>
        <div id="tempo{{$i}}" style="display:none">

        </div>
    </div>
@endforeach

I hope with this you can find something.

  • 1

    I will test and answer you, I thank you for the moral

  • 1

    tested here and continues with the same problem

  • 2

    Show me the whole Controller. In order to analyze.

  • 1

    ta ai o controller todo Douglas

  • 2

    @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 ?

  • 1

    is here the function that saves the data @Douglas pjuizfora

  • 2

    @Tulip tree, I put a similar case of multiple selects. But I really have no idea what’s going on.

Show 2 more comments

2


When retrieving data from the bank, String that was separated by comma I used the command:

explode(',', $string);

Causing String to break up But with everything she always stayed one , in the remainder.

for the solution of this I had to make a loop removing , and spaces.

$new_arr  = explode(',', $string);
    foreach($new_arr as $value)
    {
        if($value=="," || $value==" ")
        {
            continue;
        }
        else
        {
            $exer[]=$value;
        }     
    }

By doing this the error stopped occurring.

  • 2

    There are other ways to do this, such as the pop array_pop that will remove the last element of the array (being possible to save it into a variable, btw)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.