many to many. Get error during additional column saving to database
Many to many relationships in the model
public function drycleanings()
{
return $this->belongsToMany('Appdrycleaning')
->withPivot('$cart_id','drycleaning_id','q_drycleaning_id');
}
and
public function carts()
{
return $this->belongsToMany('Appcart')
->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
]
Migration for cart_drycleaning table
<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateCartDrycleaningTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cart_drycleaning', function (Blueprint $table) {
$table->increments('id');
$table->integer('cart_id');
$table->integer('drycleaning_id');
$table->integer('q_drycleaning_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('cart_drycleaning');
}
}
If I use only $cart->drycleanings()->attach($request->drycleaning_id);
then cart_id and drycleaning_id gets stored into association/pivot table (table name: cart_drycleaning)
I am trying to save q_drycleaning_id (represent quantity of item) getting as input from user.
Is there any way I can only pass non-null values? or any other solution to save both into association table?
from Laravel Questions and Answers https://laravelquestions.com/laravel/many-to-many-get-error-during-additional-column-saving-to-database/
via Lzo Media
No comments:
Post a Comment