Reputation: 8077
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
EDIT: I need to add these values to the database as either json or comma separated values - anything really, it just needs to be able to use more than one select tag that has the same name array and save all the chosen fields into the database table
Upvotes: 0
Views: 593
Reputation: 5880
The problem is that you are making use of a select
to store multiple values, but at the same time trying to store an array in a text field of your table (in the schema for the table, you have $table->text('genetics');
). The submitted form will pass an array of selected values for the field genetics
, which you can't store as text without tweaking it a bit.
To get round this, you can encode the selected values array in JSON format and then store it.
// in your controller
$values = $request->all();
$values['genetics'] = json_encode($values['genetics']); // replacing the array value to json
Gecko::create($values);
And, to retrieve the values back (e.g. when populating the select in your view) you can decode the JSON string back to an array [intuitively] with the opposite json_decode()
function.
Note: Do remember to add the multiple
attribute in your selects
to enable the user to select multiple options.
That is,
<select name="genetics[]" class="form-control" multiple>
Upvotes: 2