用工厂 table 的另一个 table 的列值填充一个 table 的列

Populate a column from one table with the column value from another table with factorys

Table ItemAA

id_aa
name_aa

Table ItemBB

id_bb
name_bb

关系一对一

要实现的目标:用相同的值填充name_aa列(ItemAA table)作为name_bb列(ItemBBtable),随机

尝试失败的工厂:

public function definition()
{
    return [
        'id_aa' => ItemBBModel::inRandomOrder()->first(), // Works great
        'name_aa' => ItemBBModel::inRandomOrder()->get(['name_bb']) // doesn't work correctly
    ];
}

已编辑:name_bb 必须与 id_bb

属于同一行

示例:

id_bb     name_bb
1          one 
2          two 
3          three

预计:

'id_aa' => 2
'name_aa' => two

我们可以稍微更新一下使其正常工作

public function definition()
{
$randomOrder = Offer::inRandomOrder()->first();
    return [
        'id_aa' => $randomOrder->id_bb, 
        'name_aa' => $randomOrder->name_bb
    ];
}