laravel 7.x一对多
laravel 7.x one to many
我的项目中有 laravel 7.x 问题
eloquent 一对多关系
我的模型 sallat 是:
public function sallproducts()
{
return $this->hasMany(sallat_product::class);
}
并且 sallat_products 模型是:
public function sallats()
{
return $this->belongsTo(sallat::class);
}
我在控制器中的输入法是:
$sallats = new sallat();
$sallats->full_total = $request->full_total;
$sallats->save();
$sall = new sallat_product();
$sall->name_product = $request->name_product;
$sall->total = $request->total;
$sall->quantity = $request->quantity;
$sall->price = $request->price;
$sall->sale = $request->sale;
$sallats->sallproducts()->save($sall);
return "done";
或者我可以这样做:
$sallats->sallproducts()->save($sall);
return "done";
然后我尝试在 postman
上测试 post 请求
{
"full_total":87997,
"arr":
[
{
"name_product":"سيتامول",
"quantity":222,
"total":299,
"price":100,
"sale":"10+2"
}
]
}
我想通过上面这种方式在一个请求中插入多条记录,
我在 postman 上有这个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sallat_id' in 'field list' (SQL: insert into sallat_products
(name_product
, total
, quantity
, price
, sale
, sallat_id
, updated_at
, created_at
) values (?, ?, ?, ?, ?, 18, 2021-07-11 02:51:59, 2021-07-11 02:51:59))
这是 sallat_products 迁移:
$table->id();
$table->bigInteger('id_sallat')->unsigned();
$table->foreign('id_sallat')->references('id')->on('sallats');
$table->string('name_product');
$table->integer('total');
$table->integer('quantity');
$table->integer('price');
$table->string('sale');
$table->timestamps();
和 sallats 迁移:
$table->id();
$table->integer('full_total');
$table->timestamps();
那我该怎么办
谁能帮我解决这个问题。
在您的 sallat_products
迁移中,它应该是:
$table->bigInteger('sallat_id')->unsigned();
$table->foreign('sallat_id')->references('id')->on('sallats');
而不是 id_sallat
以匹配 Laravel 约定。
或者,您可以将此列的名称指定为关系中 hasMany
和 belongsTo
的第二个参数。
我的项目中有 laravel 7.x 问题 eloquent 一对多关系 我的模型 sallat 是:
public function sallproducts()
{
return $this->hasMany(sallat_product::class);
}
并且 sallat_products 模型是:
public function sallats()
{
return $this->belongsTo(sallat::class);
}
我在控制器中的输入法是:
$sallats = new sallat();
$sallats->full_total = $request->full_total;
$sallats->save();
$sall = new sallat_product();
$sall->name_product = $request->name_product;
$sall->total = $request->total;
$sall->quantity = $request->quantity;
$sall->price = $request->price;
$sall->sale = $request->sale;
$sallats->sallproducts()->save($sall);
return "done";
或者我可以这样做:
$sallats->sallproducts()->save($sall);
return "done";
然后我尝试在 postman
上测试 post 请求{
"full_total":87997,
"arr":
[
{
"name_product":"سيتامول",
"quantity":222,
"total":299,
"price":100,
"sale":"10+2"
}
]
}
我想通过上面这种方式在一个请求中插入多条记录, 我在 postman 上有这个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sallat_id' in 'field list' (SQL: insert into
sallat_products
(name_product
,total
,quantity
,price
,sale
,sallat_id
,updated_at
,created_at
) values (?, ?, ?, ?, ?, 18, 2021-07-11 02:51:59, 2021-07-11 02:51:59))
这是 sallat_products 迁移:
$table->id();
$table->bigInteger('id_sallat')->unsigned();
$table->foreign('id_sallat')->references('id')->on('sallats');
$table->string('name_product');
$table->integer('total');
$table->integer('quantity');
$table->integer('price');
$table->string('sale');
$table->timestamps();
和 sallats 迁移:
$table->id();
$table->integer('full_total');
$table->timestamps();
那我该怎么办 谁能帮我解决这个问题。
在您的 sallat_products
迁移中,它应该是:
$table->bigInteger('sallat_id')->unsigned();
$table->foreign('sallat_id')->references('id')->on('sallats');
而不是 id_sallat
以匹配 Laravel 约定。
或者,您可以将此列的名称指定为关系中 hasMany
和 belongsTo
的第二个参数。