Select multiple checkbox with foreach, coming from the bank in Aravel?

Asked

Viewed 473 times

-1

I’m populating a checkbox with the bank information, I’m trying to get it to read all the checkbox selected.

Controller

    public function store(RequestProduto $request)
{
    $data = new Produtos;
    $data->nome_visivel = $request->nome_visivel;
    $data->nome = $request->nome;
    $data->pdf = $request->pdf;
    $data->keywords = $request->keywords;
    $data->save();

    return Redirect::to('adm/produto')->with('return', 'Dados salvos com sucesso');
}

View

@foreach($atributos as $key => $value)
        <input type="checkbox" name="nome[]" value="{{$value->nome}}">{{$value->nome}}
@endforeach
  • How do you know he’s selected ? I don’t quite understand what you need.

  • @Gumball I don’t just want it to save only one checkbox value, I need it to return me all the selected values

  • Is that you didn’t post the HTML complete, understood the question, each name belongs to a new product or all these names will be saved in the same product? name = checkbox’s selected, right?

  • That @Kennyrafael

  • If I leave name="name", it saves only the last check

  • But you will concatenate all of them to save or each will generate a new record?

  • I want to concatenate everyone

Show 2 more comments

1 answer

0


The parameter is received as an array, just give a implode() in the data to concatenate:

public function store(RequestProduto $request)
{
    $data = new Produtos;
    $data->nome_visivel = $request->nome_visivel;
    $data->nome = implode(' - ', $request->nome); // onde o primeiro parâmetro é o separador dos itens
    $data->pdf = $request->pdf;
    $data->keywords = $request->keywords;
    $data->save();

    return Redirect::to('adm/produto')->with('return', 'Dados salvos com sucesso');
}
  • Gave it here: implode(): Invalid Arguments passed

  • Now it was... thank you very much, I had forgotten to receive the name as array... vc saved me! Thank you very much!

  • Haha, good that you got it right, consider marking the answer as correct if it helped, you can also vote up on it for the contribution!

Browser other questions tagged

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