Laravel 5.1 - 尝试将数组保存到数据库列

时间:2015-10-21 13:03:49

标签: php arrays laravel laravel-5.1

我试图在我的应用程序中使用以下内容:

    <div class="form-group">
        <label for="genetics">Bell Albino</label>
        <select name="genetics[]" class="form-control">
            <option>N/A</option>
            <option value="BA">Visual</option>
            <option value="ba">Recessive</option>
        </select>
    </div>
    <div class="form-group">
        <label for="genetics">Tremper Albino</label>
        <select name="genetics[]" class="form-control">
            <option>N/A</option>
            <option value="TA">Visual</option>
            <option value="ta">Recessive</option>
        </select>
    </div>

您认为哪个工作正常,但是当我尝试提交表单时,我收到错误:

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

这是我的模型,不确定它是否会有所帮助:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Gecko extends Model
{
    /**
     * Fillable fields for a gecko
     * @var array
     */
    protected $fillable = [
        'name',
        'aquisition_date',
        'morph',
        'sex',
        'genetics',
        'bio',
        'bred',
        'hatchling',
        'clutch',
        'user_id'
    ];

    /**
     * A gecko has many photos
     * @return \Illuminate\Database\Eloquent\Relations\HasMany;
     */
    public function photos()
    {
        return $this->hasMany('App\GeckoPhoto');
    }

    /**
     * A gecko has many weights
     * @return \Illuminate\Database\Eloquent\Relations\HasMany;
     */
    public function weights()
    {
        return $this->hasMany('App\Weight');
    }
}

商店方法:

public function store(GeckoRequest $request)
{

    Gecko::create($request->all());

    flash()->success('Success!', 'Your gecko has been added to the system');
    return redirect()->action('GeckoController@show', [$request['name']]);

}

GeckoRequest文件:

<?php

namespace App\Http\Requests;

use Auth;
use App\Http\Requests\Request;

class GeckoRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'morph' => 'required',
            'sex' => 'required',
            'genetics' => 'required',
            'name' => "required|unique:geckos,name,NULL,id,user_id," . \Auth::user()->id
        ];
    }
}

所有保存在数据库中的都是单词Array

我确定它非常简单,但我不确定如何解决它

修改: 我需要将这些值作为json或逗号分隔值添加到数据库中 - 实际上,它只需要能够使用多个具有相同名称数组的select标记并将所有选定的字段保存到数据库表中< / p>

1 个答案:

答案 0 :(得分:2)

问题是您正在使用select来存储多个值,但同时尝试将数组存储在表的 text 字段中(在表的架构,你有$table->text('genetics');)。提交的表单将传递字段genetics的所选值的数组,您无法将其作为文本存储而无需稍微调整一下。

为了解决这个问题,你可以用JSON格式编码所选的值数组,然后存储它。

// in your controller
$values = $request->all();
$values['genetics'] = json_encode($values['genetics']); // replacing the array value to json

Gecko::create($values);

并且,要返回值(例如,在视图中填充select时),您可以使用相反的json_decode()函数[直观地]将JSON字符串解码回数组。

注意:请务必在multiple中添加selects属性,以便用户选择多个选项。 也就是说,

<select name="genetics[]" class="form-control" multiple>