I have an array which I combined using array_combine. I am trying to store each array KEY AND VALUE into a child table. However, I can't figure out to do this. Please help!
HERE'S AN EXAMPLE OF A RETURNED ARRAY array:2 [▼ "Design" => "Pattern" "Brand" => "Sony" ]
PRODUCT MODEL
public function productAttributes()
{
return $this->hasMany('App\ProductAttribute');
}
PRODUCT ATTRIBUTE MODEL
protected $fillable = [
'attribute_name', 'attribute_value', 'used_as_filter'
];
public function product()
{
return $this->belongsTo('App\Product');
}
PRODUCT CONTROLLER
$product = new Product();
$product->category_id = $request->category_list;
$product->name = $request->name;
$product->price = $request->price;
$product->save();
/**Optional Data**/
if ($request->has('attribute_name')){
$attributes = array_combine($request->input('attribute_name'), $request->input('attribute_value'));
$product->productAttributes()->create($attributes);
}
When I run this, I get one row inserted into the product table, and one row inserted into the product_attribute table. However, the columns attribute_name and attribute_value is blank.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire