如何在数据库的单个列中保存多个复选框值并使用 laravel 检索它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44289936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:04:06  来源:igfitidea点击:

How do I save multiple checkbox values in a single column in the database and retrieve it using laravel

phpdatabaselaravelcheckboxcrud

提问by siros

I have 15 Checkbox at my admin panel so only website admin can select or cancel them.

我的管理面板上有 15 个复选框,因此只有网站管理员可以选择或取消它们。

I need to save checkbox's that checked at my table like this:

我需要像这样保存在我的桌子上检查过的复选框:

Name: car,food,game,...

HTML:

HTML:

{{Form::open(['action'=>'adminHobbyController@store'])}}
    <div class="form-group">
        <label for="art" class="checkbox-inline">
            {{Form::checkbox('art[]','art')}}Art
         </label>
         <label for="artitecture" class="checkbox-inline">        
              {{Form::checkbox('artitecture[]','artitecture')}}Artitecture
         </label>
          <label for="business" class="checkbox-inline">
              {{Form::checkbox('business[]','business')}}Business
          </label>
              ...
          <div class="form-group">
              {{Form::submit('ADD',['class'=>'form-control'])}} 
          </div>
 {{Form::close()}}

My Controller Store Function :

我的控制器存储功能:

 public function store(Request $request)
 {
    $add_hobby=new Hobbies;
    $add_hobby->name=$request->all();
    $add_hobby->save();
    return redirect()->back();
 }

Also try this but only save the last one :

也试试这个,但只保存最后一个:

public function store(Request $request)
 {
    $add_hobby=new Hobbies;
    $add_hobby->name=$request->input('car');
    $add_hobby->name=$request->input('food');
      ...
   $add_hobby->name=$request->input('fashion');
    $add_hobby->save();
    return redirect()->back();
 }

I tried this too but I got Error :

我也试过这个,但我得到了错误:

 public function store(Request $request)
{
    $request->merge([
    'name' => implode(',', (array) $request->input('game')),
    'name' => implode(',', (array) $request->input('food')),
      ...
      'name' => implode(',', (array) $request->input('fashion')),
]);

    $add_hobby=new Hobbies;
    $add_hobby->name=$request->input()->all();
    $add_hobby->save();
    return redirect()->back();
}

Anyone can help?

任何人都可以帮忙吗?

Of course is not necessary save at one column but also i don't know another way to save them

当然没有必要保存在一列,但我也不知道另一种保存它们的方法

回答by Vilius

You're creating multiple arrays by doing this

您正在通过这样做创建多个数组

{{Form::checkbox('art[]','art')}}Art

Insted of art[]use seomthing like hobby[]and then put artas the value

Instedart[]使用 seomthing likehobby[]然后把它art作为值

Try this

尝试这个

    <label for="art" class="checkbox-inline">
        {{Form::checkbox('hobby[]','art')}}Art
     </label>
     <label for="artitecture" class="checkbox-inline">        
          {{Form::checkbox('hobby[]','artitecture')}}Artitecture
     </label>
      <label for="business" class="checkbox-inline">
          {{Form::checkbox('hobby[]','business')}}Business
      </label>
          ...
      <div class="form-group">
          {{Form::submit('ADD',['class'=>'form-control'])}} 
      </div>

And then in your controller you would go something like

然后在你的控制器中你会去像

foreach ($request->input("hobby") as $hobby){
        $add_hobby = new Hobbies;
        $add_hobby->name= $hobby;
        $add_hobby->save();
}

回答by Rubanraj Ravichandran

You could do a simple step to save this in a one shot.

你可以做一个简单的步骤来一次性保存它。

 <label for="art" class="checkbox-inline">
    {{Form::checkbox('art','art')}}Art
 </label>
 <label for="artitecture" class="checkbox-inline">        
      {{Form::checkbox('artitecture','artitecture')}}Artitecture
 </label>
  <label for="business" class="checkbox-inline">
      {{Form::checkbox('business','business')}}Business
  </label>
      ...
  <div class="form-group">
      {{Form::submit('ADD',['class'=>'form-control'])}} 
  </div>

And then in your controller,

然后在你的控制器中,

$hobby = implode(",",array_keys($request->except(['_method','_token'])))
//Exclude the parameters from the $request using except() method
//now in your $hobby variable, you will have "art,artitecture,business"

$add_hobby=new Hobbies;
$add_hobby->name=$hobby;
$add_hobby->save();

Dont forget to exclude the data that you dont need for hobbies.

不要忘记排除您不需要的数据。

回答by Disturb

you can use the implode method and store it in a variable then assign it to the column.

您可以使用内爆方法并将其存储在变量中,然后将其分配给列。

$add_hobby->[column]= $array_imploded;

回答by Braggae

First, make sure that column in which you want to store those values is varchar or text and can store all the text you'll put there.

首先,确保要在其中存储这些值的列是 varchar 或 text,并且可以存储您将放置在那里的所有文本。

Second, make a JSON string out from your values. (Or serialized array string but JSON is better to read). Then just store this setting as a string and do not forget to parse JSON back to an array when you access this data.

其次,从您的值中生成一个 JSON 字符串。(或序列化的数组字符串,但 JSON 更好读)。然后将此设置存储为字符串,并且在访问此数据时不要忘记将 JSON 解析回数组。

This links will help you a bit:

此链接将对您有所帮助:

http://php.net/manual/en/function.json-encode.php

http://php.net/manual/en/function.json-encode.php

http://php.net/manual/en/function.json-decode.php

http://php.net/manual/en/function.json-decode.php

and for the case, you pick a seralized array: http://php.net/manual/en/function.serialize.php

对于这种情况,您选择一个序列化数组:http: //php.net/manual/en/function.serialize.php