1
我正在試圖在種子中使用ID數據透視表。播種和數據透視表?
$id = DB::table('products')->insertGetId(array(
array(
'title' => 'Product A',
'published' => 1
)
));
DB::table('product_user')->insert(array(
array(
'product_id' => $id,
'user_id' => '9999999999'
)
));
以上是最好的方法嗎?通過insertGetId獲得一個id,然後把它放在數據透視表中有沒有更好的方法?
而且上面的方式給出了一個錯誤:
[ErrorException]
preg_replace(): Parameter mismatch, pattern is a string while replacement i
s an array
我懷疑的$ id是一個數組,我怎麼能得到InsertGetId返回一個int?
謝謝,你如何插入這個例子的數據,然後得到它的ID,所以你可以使用它的查找? – panthro
您需要已經插入'user'和'product'。你可以通過聲明一個新的產品/用戶模型對象來實現。 '$ product = new Product;',把數據存入''product-> save()'e,然後通過$ product-> id'來保存。同樣適用於'user'。 –
謝謝,但正如最初所問,這將如何與種子工作? – panthro