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条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据