将数组值插入数据库laravel

时间:2015-09-08 09:24:23

标签: php mysql arrays laravel-5

我有一个名为namedescriptionfeatures的表单属性,其中features是多个复选框,类似于

feature 1
feature 2
feature 2
feature 4

用户可以一次选择多个复选框。我有一个名为product的数据库表,如

-----------------------------------------
    id    name    description    features  
-----------------------------------------

当用户选择多个复选框时,我需要在features列中插入所有复选框值。现在,我可以echo选中的复选框值,如

 $feat = Input::get('features');
    foreach ($feat as $key => $n){
     echo $feat[$n];
    }

但我需要将这些功能插入到数据库中,对于正常插入,我们会这样做:

$product = new Product;
$product->name = Input::get('name');
$product->description = Input::get('description');
$product->features = Input::get('features');
$product->save();

但是我应该如何修改上面的代码以便将数组值保存到数据库中?我试图将值插入到同一列,因为我不会根据功能查询它。

5 个答案:

答案 0 :(得分:2)

这很简单。如果您知道自己不会查询这些功能,那么将它们存储为Json或序列化数组就完全没问题了。但是如果你需要查询它们并且它们是你的应用程序的一个关键方面,你应该将它们放在自己的表中。

我更喜欢以Json格式存储数组,因为它更容易阅读,并不是特定于PHP。 Laravel为您提供了非常好的选择。

首先,在您的迁移中将features - products - 字段声明为json:

Schema::create('products', function (Blueprint $table) {
    // ...
    $table->json('features');
});

然后通过简单地设置Product - 属性来告诉您的$casts - 模型到automatically cast it到数组:

class Product extends Model
{
    // ...
    protected $casts = [
        'features' => 'json'
    ];
}

那就是它。现在数组将存储为Json,但是当您使用$product->features访问它时,您将获得一个数组。

为了使一切变得更加简单,您应该在产品型号上设置fillable attribute

class Product extends Model
{
    // ...
    protected $fillable = ['name', 'description', 'features'];
}

这允许在您的控制器中(或在您创建产品的任何地方)执行此类操作:

$product = Product::create(Input::all());

...而不是新建它并逐个设置属性。

如上所述,请确保您不需要这些功能可以查询,这意味着您不会遇到某些情况,即您尝试使某些产品只具有特定功能或一些东西。但是,如果您确实需要查找功能,则应将它们放在自己的表中。否则这种方法很好,因为它更方便。

答案 1 :(得分:1)

您可以使用json_encode函数将数组保存为json字符串。

$product->features = json_encode(Input::get('features'));

但我也同意@AdrianBR将数组值保存到单个列中的数据库是一个坏主意。

答案 2 :(得分:1)

请查看database normalization以获取有关如何存储1:N关系的数据的信息。您必须创建一个额外的表,例如“product_feature”,其属性为:['id','product_id','function_name']。

然后你的插入函数如下所示:

$product = new Product;
$product->name = Input::get('name');
$product->description = Input::get('description');
$product->save();

$feat = Input::get('features');

foreach ($feat as $key => $n){
    $product_function = new ProductFunction;
    $product_function->product_id = $product->id;
    $product_function->function_name = $feat[$n];
    $product_function->save();
}

希望它有所帮助!

答案 3 :(得分:0)

尽管我使用ajax发送值,但我也有类似的情况

<div class="row mb-3" id="insert_new_item">
<div class="col-lg-6">
<p>Item Description</p>
<input value="" type="text" name="order[description]"  class="form-control product " id="" placeholder="Your Item Description">
</div>
<div class="col-lg-1 col-sm-6 col-12 mt-lg-0 mt-3">
    <p>Qty</p>
    <input placeholder="0" type="text" name="order[quantity]"  min="1" class="text-right form-control qty" id="" min="1">
</div>
<div class="col-lg-2 col-sm-6 col-12 mt-lg-0 mt-3">
    <p>Rate/Unit (&#8358; )</p>
    <input placeholder="0.00" type="text" name="order[rate]" class="text-right form-control rate" id="">
</div>
<div class="col-lg-2 mt-lg-0 mt-3">
    <p>Amount (&#8358; )</p>
    <input  type="text" name="order[amount]" id="" class="text-right form-control amount" readonly value="0" >
</div>
<div class="col-lg-1">
    <span><i class="fa fa-times text-danger  removeItem" style="font-size: 22px"></i></span>
</div>

所以当我console.log(任何值)时,我得到了:

["0" : "value 1", "1" : "value 2"]

我可以通过控制器执行此操作

foreach ($request->description as $key => $description) {
    $order = Order::create([
        'description' => $description,
        'amount' => $request->rate[$key],
        'quantity' => $request->quantity[$key],
        'invoice_id' => 7
     ]);

}

答案 4 :(得分:0)

您可以这样插入

$ candidate_data = array(

            'mobile' => $request->get('mobile'),
            'password' => $request->get('password')
    );

DB::('表名')->插入($ candidate_data);