Home > Software engineering >  Populate a column from one table with the column value from another table with factorys
Populate a column from one table with the column value from another table with factorys

Time:04-09

Table ItemAA

id_aa
name_aa

Table ItemBB

id_bb
name_bb

RelashionShips One To One

Goal to be achieved: populate the name_aa column (ItemAA table) with the same value as the name_bb column (ItemBB table), randomly

Factory with unsuccessful attempt:

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

EDITED: The name_bb must belong to the same row as the id_bb

Example:

id_bb     name_bb
1          one 
2          two 
3          three

Expected:

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

CodePudding user response:

we can update it a little bit to make it works

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