douying3251 2018-12-24 13:45
浏览 129
已采纳

可以在我的create方法中添加数组吗?

Trying to create a new record for my Account model.

My model has the following structure: 'title','information','image','combat','quest','price' I also have 24 more items on my model that are considered as skills

I want to make it possible that for those 24 items I can make a loop in my create method, so that I don't have to manually add all the skills in my create.

Model looks like the following:

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();
    });
}

My create method:

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? 

    ]);

}
  • 写回答

2条回答 默认 最新

  • dt20081409 2018-12-24 13:54
    关注

    Give the same names to the input fields as table columns after that you can create record using the following. It should work

    $mergedArray = array_merge($account, $skills);
    Account::create($mergedArray);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改