Db:一对多关系laravel的种子和传递变量
我想建立一个数据库,我有两个问题 1) 当存在一对多关系时,我希望在新的种子设定类中传递另一个类(上一个表)的参数。我怎样才能做到这一点 2)Db:一对多关系laravel的种子和传递变量,laravel,laravel-4,Laravel,Laravel 4,我想建立一个数据库,我有两个问题 1) 当存在一对多关系时,我希望在新的种子设定类中传递另一个类(上一个表)的参数。我怎样才能做到这一点 2) $row1->table2()->附加($row2->id)我用它实现的是将表2的所有id附加到表1,对吗 编辑: 我有这段代码,问题是id_cat字段来自另一个表,是我已经播种的另一个类的表,所有这些都在数据库播种器类中播种,允许我像这样重用变量: class DatabaseSeeder extends Seeder { /**
$row1->table2()->附加($row2->id)代码>我用它实现的是将表2的所有id附加到表1,对吗
编辑:
我有这段代码,问题是id_cat字段来自另一个表,是我已经播种的另一个类的表,所有这些都在数据库播种器类中播种,允许我像这样重用变量:
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Eloquent::unguard();
$this->call('Seeder');
$this->command->info('Seed complete.');
}
}
class CatTableSeeder extends Seeder {
public function run() {
$catFintaPelle = Cat::create(array(
'path_img' => '/img/Materiale Imbottitura'
));
}
}
class LanguageTableSeeder extends Seeder {
public function run() {
$lingua_it_catFintapelle= Linguacat::create(array(
'nome' => 'Finta pelle',
'descrizione' => 'Finta pelle nautica/Finta pelle tempo libero/Finta pelle aviazione/Finta pelle automoto/finta pelle contract/finta pelle uffici/finta pelle esterni',
'lingua' => 'it-IT',
'id_cat' => $catFintaPelle->id
));
}
}
$ids = OtherTable::all()->lists('id'); // this will generate an array of ids
$row1->table2()->sync($ids); // sync the ids with the relation
在这里,我可以在新种子中重用种子的id变量,节省我的时间。
但我在某个地方读到,每个表的每个种子都应该有hos自己的类。当您进行种子设定时,假设您正在对新的数据库进行种子设定,因此您只需硬编码值,而不是从另一个表加载它们
但是,如果要附加所有ID,可以执行以下操作:
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Eloquent::unguard();
$this->call('Seeder');
$this->command->info('Seed complete.');
}
}
class CatTableSeeder extends Seeder {
public function run() {
$catFintaPelle = Cat::create(array(
'path_img' => '/img/Materiale Imbottitura'
));
}
}
class LanguageTableSeeder extends Seeder {
public function run() {
$lingua_it_catFintapelle= Linguacat::create(array(
'nome' => 'Finta pelle',
'descrizione' => 'Finta pelle nautica/Finta pelle tempo libero/Finta pelle aviazione/Finta pelle automoto/finta pelle contract/finta pelle uffici/finta pelle esterni',
'lingua' => 'it-IT',
'id_cat' => $catFintaPelle->id
));
}
}
$ids = OtherTable::all()->lists('id'); // this will generate an array of ids
$row1->table2()->sync($ids); // sync the ids with the relation
您能否更清楚地了解如何将id与第二个comman同步,我查看了文档,并说它仅用于多对多关系对不起,我以为您在做多对多。由于您正在播种数据,只需对其进行硬编码,如下所示$row1->table2()->attach(1)