尝试为我的Account
模型创建新记录。
我的模型具有以下结构:'title','information','image','combat','quest','price'
我的模型中还有24个项目被认为是skills
我想让这24个项目可以在我的create方法中循环,这样我就不必在我的创建中手动添加所有技能。
模型如下所示:
public function up()
{
Schema::create('accounts', function (Blueprint $table) {
$table->increments('id');
$table->integer('price');
$table->string('image_url');
$table->integer('quest_points');
$table->string('title');
$table->string('information');
$table->integer('attack');
$table->integer('defence');
$table->integer('strength');
$table->integer('constitution');
$table->integer('cooking');
$table->integer('construction');
$table->integer('farming');
$table->integer('crafting');
$table->integer('firemaking');
$table->integer('fishing');
$table->integer('fletching');
$table->integer('herblore');
$table->integer('hunter');
$table->integer('magic');
$table->integer('mining');
$table->integer('prayer');
$table->integer('ranged');
$table->integer('runecrafting');
$table->integer('slayer');
$table->integer('smithing');
$table->integer('agility');
$table->integer('thieving');
$table->integer('woodcutting');
$table->integer('total_level');
$table->integer('combat_level');
$table->timestamps();
});
}
我的创建方法:
public function store(Request $request)
{
$account = $request->all('title','description','image','combat','quest','price');
$skills = $request->get('skill');
// array of 24 items, example -> 'attack' = 52;
Account::create([
'title' => $account['title'],
'price' => $account['price'],
'information' => $account['description'],
'image_url' => $account['image'],
'combat_level' => $account['combat'],
'quest_points' => $account['quest'],
// I would do the following normally
'attack' => $skills['attack'],
'defence' => $skills['defence'],
// ....
// possible to do this with a loop of the array $skills?
]);
}
答案 0 :(得分:4)
为输入字段提供与表列相同的名称,然后可以使用以下内容创建记录。它应该可以工作
$mergedArray = array_merge($account, $skills);
Account::create($mergedArray);
答案 1 :(得分:3)
首先请确保输入字段的名称与表记录的名称相同,然后您可以使用以下命令创建记录:
Account::create($skills);
如果您想在帐户模型中插入许多技能,可以使用insert
方法
$skills = array(
$array_of_skills,
$array_of_skills_,
//...
);
Account::insert($skills);