在laravel中的同一个表中添加多行

时间:2017-03-23 22:55:42

标签: php mysql laravel

如何使用foreach在laravel中的同一个表中插入双行,如果language = en插入第一行,并且如果language = ar插入具有相同存储函数的第二行,则需要添加双行,这里是我的代码控制器:

$parentproduct = new Product();
        $parentproduct->id = Input::get('id');
        $parentproduct->save();
        $insertedId = $parentproduct->id;

        $allProducts= new ProductsTranslation();
        foreach($allProducts as $product->language)
        {
            $product = new ProductsTranslation();   
            $product->id = Input::get('id');
            $product->product_id = $insertedId;
            $product->title = Input::get('title');
            $product->content = Input::get('content');
            $product->price = Input::get('price');
            $product->description_title = Input::get('description_title');
            $product->prod_info_title = Input::get('prod_info_title');
            $product->prod_info = Input::get('prod_info');
            $product->save();
        }

我在我的刀片上添加了这个

 <input type="text" name="language"  value="ar" disabled>

但是我收到了错误:

  

ProductsController.php第169行中的ErrorException:   从空值

创建默认对象

这是我的刀片形式:

 {{ csrf_field() }}

                  <div class="form-group">
                    {!!Form::label("Product Title", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}

                    <div class="col-md-6 col-sm-6 col-xs-12">
                    {!!Form::text('title'," ",["class"=>"form-control col-md-7 col-xs-12" ,"id"=>"last-name"])!!} 

                    </div>
                  </div>
                   <div class="form-group">
        {!!Form::label("Product Language", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}

                    <div class="col-md-6 col-sm-6 col-xs-12">

        <input type="text" name="language"  value="en" disabled>.

                    </div>
                  </div>
                   <div class="form-group">
                    {!!Form::label("Product Content", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                    <div class="col-md-6 col-sm-6 col-xs-12">
              <textarea class="form-control" rows="3"  name="content"></textarea>
                    </div>
                  </div>
                   <div class="form-group">
                    {!!Form::label("Product Price", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                    <div class="col-md-6 col-sm-6 col-xs-12">
                    {!!Form::text('price'," ",["class"=>"form-control col-md-7 col-xs-12" ,"id"=>"last-name"])!!} 
                    </div>
                  </div>
                  <div class="form-group">
                {!!Form::label("Description Title", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                    <div class="col-md-6 col-sm-6 col-xs-12">
                    {!!Form::text('description_title'," ",["class"=>"form-control col-md-7 col-xs-12" ,"id"=>"last-name"])!!} 
                    </div>
                  </div>

                   <div class="form-group">
        {!!Form::label("The Product Description", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                    <div class="col-md-6 col-sm-6 col-xs-12">
      <textarea class="form-control" rows="3"  name="description"></textarea>
                    </div>
                  </div>

                   <div class="form-group">
                {!!Form::label("The Product Info Title", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                    <div class="col-md-6 col-sm-6 col-xs-12">
      {!!Form::text('prod_info_title'," ",["class"=>"form-control col-md-7 col-xs-12" ,"id"=>"last-name"])!!} 
                    </div>
                  </div>

                   <div class="form-group">
        {!!Form::label("The Product Info", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                    <div class="col-md-6 col-sm-6 col-xs-12">
      <textarea class="form-control" rows="3"  name="prod_info"></textarea>
                    </div>
                  </div>  <div class="ln_solid"></div>

                  <div class="ar">
                          <div class="form-group">

                    <div class="col-md-6 col-sm-6 col-xs-12">
        {!!Form::text('title'," ",["class"=>"form-control col-md-7 col-xs-12" ,"id"=>"last-name"])!!} 
        <input type="text" name="language"  value="ar" >

                    </div>
                      {!!Form::label("عنوان المنتج", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                  </div>
         <div class="form-group">

                    <div class="col-md-6 col-sm-6 col-xs-12">
      <textarea class="form-control" rows="3"  name="content"></textarea>
                    </div>
                    {!!Form::label("محتوي المنتج", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                  </div>
         <div class="form-group">

                    <div class="col-md-6 col-sm-6 col-xs-12">
        {!!Form::text('price'," ",["class"=>"form-control col-md-7 col-xs-12" ,"id"=>"last-name"])!!} 
                    </div>
                      {!!Form::label("سعر المنتج", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                  </div>
                  <div class="form-group">

                    <div class="col-md-6 col-sm-6 col-xs-12">
        {!!Form::text('description_title'," ",["class"=>"form-control col-md-7 col-xs-12" ,"id"=>"last-name"])!!} 
                    </div>
                     {!!Form::label("عنوان وصف المنتج", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                  </div>

                   <div class="form-group">

                    <div class="col-md-6 col-sm-6 col-xs-12">
      <textarea class="form-control" rows="3"  name="description"></textarea>
                    </div>
                      {!!Form::label("وصف المنتج", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                  </div>


                   <div class="form-group">

                    <div class="col-md-6 col-sm-6 col-xs-12">
      {!!Form::text('prod_info_title'," ",["class"=>"form-control col-md-7 col-xs-12" ,"id"=>"last-name"])!!} 
                    </div>
                      {!!Form::label("عنوان بيانات المنتج", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                  </div>

                   <div class="form-group">

                    <div class="col-md-6 col-sm-6 col-xs-12">
      <textarea class="form-control" rows="3"  name="prod_info"></textarea>
                    </div>
                      {!!Form::label("بيانات المنتج ", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                  </div>

              <div class="form-group">

                    <div class="col-md-6 col-sm-6 col-xs-12">
        {!!Form::file('main_image',["class"=>"form-control col-md-7 col-xs-12" ])!!} 
                    </div>
                     {!!Form::label("الصورة الرئيسية للمنتج", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                  </div>
                   <div class="form-group">

                    <div class="col-md-6 col-sm-6 col-xs-12">
        {!!Form::file('image_1',["class"=>"form-control col-md-7 col-xs-12" ])!!} 
                    </div>
                    {!!Form::label("الصورة الفرعية الأولي للمنتج",  null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                  </div>
                  <div class="form-group">

                    <div class="col-md-6 col-sm-6 col-xs-12">
        {!!Form::file('image_2',["class"=>"form-control col-md-7 col-xs-12" ])!!} 
                    </div>
                    {!!Form::label("الصورة الفرعية الثانية", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                  </div>
                  <div class="form-group">

                    <div class="col-md-6 col-sm-6 col-xs-12">
        {!!Form::file('image_3',["class"=>"form-control col-md-7 col-xs-12" ])!!} 
                    </div>
                     {!!Form::label("الصورة الفرعية الثالثه", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                  </div>
                  <div class="form-group">

                    <div class="col-md-6 col-sm-6 col-xs-12">
        {!!Form::file('image_4',["class"=>"form-control col-md-7 col-xs-12" ])!!} 
                    </div>
                     {!!Form::label("الصورة الفرعية الرابعة", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                  </div>

                  <div class="form-group">
                    {!!Form::label("Select Product number", null, ['class' => 'control-label col-md-3 col-sm-3 col-xs-12']) !!}
                    <div class="col-md-6 col-sm-6 col-xs-12">



                    </div>

                  </div>

                  </div>


                    <div class="ln_solid"></div>
                  <div class="form-group">
                    <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                    {!!Form::submit('Cancel',["class"=>"btn btn-sucess"])!!}

         <input type="submit" value="store" name="submit" class="btn btn-primary">
                    </div>
                  </div>
                </form>

0 个答案:

没有答案