-1

I'm trying to insert records to pivot table from checkboxes and texboxes. Foreach checkbox has one textbox attached.

My blade:

@foreach(Stock::all() as $stock)
<div class="form-check">                                
  <input class="form-check-input stockCheckbox" type="checkbox" name="stock_id[]" value="{{$stock->id}}" id="{{$stock->id}}">
  <label class="form-check-label" for="stock_id">
    {{$stock->name}}
  </label>     
</div>
<div class="form-group" id="qtySpentField">
  <input type="text" class="form-control" name="qtySpent" id="qtySpent" value="" placeholder="qtySpent">
</div>  
@endforeach

Model for Products:

class Produktet extends Model
{
     public function stocks()
    {
        return $this->belongsToMany('App\Stock')->withPivot('qtySpent')->withTimestamps();
    }

}

Model for Stock:

class Stock extends Model
{
    public function produktets(){
        return $this->belongsToMany('App\Produktet')->withTimestamps();
    }
}

My pivot table:

Schema::create('produktet_stock', function (Blueprint $table) {

           $table->bigIncrements('id');        
    $table->string('produktet_id');
    $table->foreign('produktet_id')
          ->references('id')
          ->on('produktets')->onDelete('cascade');
   
    $table->string('stock_id');
    $table->foreign('stock_id')
          ->references('id')
          ->on('stocks')->onDelete('cascade');               
            $table->double('qtySpent', 8, 2);
            $table->timestamps();
    });

As you can see, I have an additional column in my pivot table called "qtySpent"

My controller:

$prod = new Produktet;
$prod->stocks()->sync($req->input('stock_id'), $req->input('qtySpent'));

The problem is that it is inserting only stock_id values in multiple rows, but in the qtySpent column it is not inserting any value only null.

    id| produktet_id | stock_id | qtySpent
    =====================================
    1   1              1          null 
    2   1              2          null  
    3   1              3          null  

So, I have to fill pivot table with stock_id values and qtySpent values in multiple rows. For example: When I check two checkboxes(for stock_id), I will write also qtySpent values in text fields and these records should be inserted in pivot table. The pivot table should fill like this:

id| produktet_id | stock_id | qtySpent
=====================================
1   1              1          0.1 
2   1              2          0.1 
3   1              3          0.2  
  • 1
    Duplicate of [Laravel attach pivot to table with multiple values](https://stackoverflow.com/questions/23226802/laravel-attach-pivot-to-table-with-multiple-values). This correctly shows proper syntax. In your case, it would be `$prod->stocks()->sync([$req->input('stock_id') => ['qtySpent' => $req->input('qtySpent')]]);` – Tim Lewis Aug 25 '21 at 15:22

1 Answers1

1

You need to use sync() function like this:


$prod->stocks()->sync([$req->input('stock_id') => ['qtySpent' => $req->input('qtySpent')]]);

More info : Syncing Associations

DG_
  • 246
  • 1
  • 3
  • 15
  • After that, I've got an error: Illegal offset type – Edie Polpa Aug 25 '21 at 21:42
  • I changed a little like this: `$manyToMany = array(); for ( $i=0 ; $i< count($req->stock_id); $i++ ) { $manyToMany[ $req->stock_id[$i] ] = ['qtySpent' =>$req->qtySpent[$i] ]; } $prod->stocks()->sync($manyToMany);` and it is working now. Thank you @Tim Lewis & @DG_ – Edie Polpa Aug 25 '21 at 22:06
  • Although in this way only works if all checkboxes are checked and fields are filled, otherwise if you select only one checkbox and fill in one field or just some of them, the field value is NULL. – Edie Polpa Sep 02 '21 at 12:28