mercredi 25 avril 2018

many to many. Get error during additional column saving to database

Many to many relationships in the model

  public function drycleanings()
    {
      return $this->belongsToMany('App\drycleaning')
      ->withPivot('$cart_id','drycleaning_id','q_drycleaning_id');
    }

and

  public function carts()
  {
    return $this->belongsToMany('App\cart')
    ->withPivot('$cart_id','drycleaning_id','q_drycleaning_id');
  }

In the controller I am using

$cart->drycleanings()->attach($request->drycleaning_id,[$request->q_drycleaning_id]);

if gives me an error

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'

Result of dd($request->all());

"drycleaning_id" => array:2 [▼
    0 => "3"
    1 => "4"
  ]
  "q_drycleaning_id" => array:58 [▼
    0 => "2"
    1 => "2"
    2 => null
    3 => null
    4 => null
    5 => null
    6 => null
    7 => null
    8 => null
    9 => null
    10 => null
  ]

Is there any way I can only pass non-null values? or any other solution to save both into association table?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire