从 laravel 中的数组插入到 table

Inserting into a table from the array in laravel

我正在从视图中获取这个数组

当我做的时候return $BlogData['Tag'];

结果是

 ["1", "2"]

我如何将其插入每条记录,即 table 中的新条目?

S.No | Tag
   3 |  1
   4 |  2

像上面给出的table

当我尝试时

foreach ($BlogData['Tag'] as $x) 
        {
        echo $x;
        Tag::create($x);
        }

我遇到了这个错误

message: "Argument 1 passed to Illuminate\Database\Eloquent\Model::create() must be of the type array, string given, 
foreach ($BlogData['Tag'] as $x) 
{
    Tag::create(['Tag' => $x]);
}
foreach ($BlogData['Tag'] as $x) 
{
    # Tag index will be the table column name
    # and its value is $x
    $pass = array(
        'Tag' => $x
    );
    Tag::create($pass);
}

这是插入多行的好方法 link 使用 laravel

Bulk Insertion in Laravel using eloquent ORM

对于 Model::create() 参数必须是数组。您传递的是单个值而不是数组。请看一下http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Model.html#method_create

对于创建方法,您必须在模型中使用 $fillable 数组

protected $fillable = [
    'Tag'
];

在你的控制器中

$data = $BlogData['Tag'];
foreach ($data as $key=>$value)
{  $data_attribute = array('Tag'=>$value);
   Tag::create($data_attribute);
}