从数据库Laravel中的db插入数据

时间:2017-03-25 22:24:48

标签: php arrays laravel

//controller function
public function storeBaritems()
{
    $id = Auth::user()->id;
    $bartypearray = array();
    $bartypes = request('select1');     // <select id="select1" name="select1[]"
    $r = 0;
    foreach ($bartypes as $type) {
        $bartypearray[$r] = Bartype::select('bartype_id')
                            ->where('bar_type_name','like',$type)
                            ->where('restaurant_id','like',$id)
                            ->get();
        $r += 1;
    }
    $baritems = request('itemname');   //<input type="text" class="hookah-field" name="itemname[]">
    $descriptions = request('description');  //<input type="text" class="hookah-field" name="description[]">
    $quantity = request('quantity'); //<input type="text" class="hookah-field" name="quantity[]">
    $prices = request('price');  //<input type="text" class="hookah-field" name="price[]">
    $i = 0;
    foreach ($baritems as $item) {
            Bar_menu::create([
                'item_name'=>$item,      
                'description'=>$descriptions[$i], 
                'quantity'=>$quantity[$i],
                'price'=>$prices[$i],
                'res_id'=>$id,
                'type_id'=>$bartypearray[$i]
                ]);
            $i += 1;
    }
} 

HTML是动态表单,它生成具有相同&#34; name&#34;的新输入字段。属性。从路由调用函数后,我收到此错误,如截图 error screenshot

我认为问题在于在bartypearray[]中插入数据。任何解决方案?

1 个答案:

答案 0 :(得分:0)

在您的第一个foreach循环中,当您尝试使用数据填充$ bartypearray时,实际上是使用对象数组填充它。这就是为什么你得到一个&#39; []&#39;作为输入而不是整数。

我建议你做的是在第二个foreach循环中访问bartypeid,如下所示: &#39; type_id&#39; = $ bartypearray [$ i] - &gt; bartypeid;

我认为它应该有效..

相关问题