mercredi 21 octobre 2015

Submitting array in laravel gives error - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

I'm trying to use the following in my application:

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

Which you would assume works okay, however when I try submitting my form, I get the error:

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

This is my model, not sure if it will help:

<?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');
    }
}

The store method:

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']]);

}

The GeckoRequest file:

<?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
        ];
    }
}

All it saves into the database is the word Array.

I'm sure it's super simple, but I'm unsure on how to fix it



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire