简体   繁体   中英

How to insert an array into different rows?

I tried all the solutions that i found during my research but nothing seems to work for me. Here's an overview of what i'm trying to do. As an admin i control the activities of the customer. The customer is the one who makes requests and i as the admin approve that request.
Tables
Packages
id | customer_id | bearer_id | status
Package_products
id | package_id |product_id | qty

blade
In my blade i made a foreach to display the current products that are currently on the cart of a customer. This blade is already on the page of the currently selected customer.

<div class="panel-wrapper collapse in">
    <div class="panel-body">
        {!!Form::open(array('route'=>'storeProduct', 'id' => 'example-advanced-form', 'method' => 'post'))!!}
            <div class="table-wrap">
                <div class="table-responsive">
                    <table id="datable_1" class="table table-hover display  pb-30" >
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>Name</th>
                                    <th>Qty</th>
                                    <th class="column-spacer"></th>
                                </tr>
                            </thead>
                                <input type="hidden" name="customerid" value="{{$customers->id}}">
                                    @foreach($customers->products as $product)
                                        <tbody>
                                            <tr>
                                                <td><input type="hidden" name="productid[]" value="{{$product->id}}">{{$product->name}}</td>
                                                <td><input type="hidden" name="qty[]" value="{{$product->pivot->qty}}">{{$product->pivot->qty}}</td>
                                            </tr>
                                        </tbody>
                                        @endforeach
                                    </table>
                                    <br>
                                </div>
                                @include('errors.list')
                                <fieldset>
                            <div class="row">
                            <center>{!!Form::submit('Process', array('class' => 'btn btn-primary'))!!}</center>
                        </div>  
                    </fieldset>
                </div>
            {!!Form::hidden('_token', csrf_token())!!}
            {!!Form::close()!!}
        </div>
    </div>

I used dd() and it shows the array of products. I can insert the requested data to the packages table with no problem, but when inserting the array of products to the package_products table it only inserts the very last row. I need to insert those products into different rows. What am i lacking?
controller

public function storeProduct(Request $request)
{
  try{
    $package = $this->package;
    $productid = $request['productid'];
    $qty = $request['qty'];
    $bearer = Bearer::status()->package()->first();
    $package->customer_id = $request['customerid'];
    $package->bearer_id = $bearer->id;
    $package->status = 'pending';
    $package->save();

    if ($package->save()){
      $id = $package->id;
      $packageproducts = [];

      $packageproducts = [
        'package_id'=>$id,
        'product_id'=>$productid,
        'qty'=>$qty];
        DB::table('package_products')->insert($packageproducts);
      }
      return redirect()->route('index')->with('message', 'Package Successfully Added');
    }
    catch(Exception $e){
      return redirect()->back()->withErrors(['Error']);
    }
  }

Assuming you have set up the eloquent models correctly in relation between the package and the product model, it would be as simple as this.

$package = App\Models\Package::first();

$package->products()->attach($packageProducts);

I finally found the solution, what i was missing was putting a for loop inside my function so it gets the array i want and saves it into different rows in my database. Here's my answer

public function storeProduct(Request $request)
{
try{
$package = $this->package;
$productid = $request['productid'];
$qty = $request['qty'];
$bearer = Bearer::status()->package()->first();
$package->customer_id = $request['customerid'];
$package->bearer_id = $bearer->id;
$package->status = 'pending';

if ($package->save()){
  $id = $package->id;
  $packageproducts = [];
  for(i=0; < count($productid); $i++){
  $packageproducts[] = [
    'package_id'=>$id,
    'product_id'=>$productid[$i],
    'qty'=>$qty[$i],];
    }
    DB::table('package_products')->insert($packageproducts);
  }
  return redirect()->route('index')->with('message', 'Package Successfully Added');
}
catch(Exception $e){
  return redirect()->back()->withErrors(['Error']);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM