我正在开发Laravel 5.7版本。我有StockModel
与其他模型有关系。 $stockModel->save()
出现控制器错误。
SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'category'
MySql错误消息:SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“类别”(SQL:更新
stock
设置了stock_qty
= 99,updated_at
= 2019-05-18 06:33:16,category
= {“ category_id”:1,“ category_name”:“女士手提包。”,“ parent_level”:0,“ category_sequence”:1,“ is_deleted” :0,“ created_at”:“ 2019-03-09 10:07:52”,“ updated_at”:“ 2019-03-09 12:13:06”},product
= {“ product_id”:1 ,“ category_id”:1,“ product_name”:“ PERINITA HB”,“ product_code”:“ 00”,“ product_sequence”:1,“ is_deleted”:0,“ created_at”:“” 2019-03-10 15:09: 12“,” updated_at“:” 2019-03-13 09:24:19“},productVariant
= {” product_variant_id“:3,” product_id“:1,” variant_name“:”粉红色“,” variant_code “:” HAFD051094M3“,” variant_sequence“:0,”已删除“:0,” created_at“:” 2019-03-13 09:24:01“,” updated_at“:” 2019-03-13 09:24:19 “},inward
= {” inward_id“:2,” inward_no“:null,” date_of_purchase“:null,” supplier_name“:”新供应商“,” supplier_invoice_no“:” as“,” supplier_invoice_date“:null ,“ invoice_amt”:0,“已删除”:0:“ created_at”:“ 2019-04-21 11:59:47”,“ updated_at”:“ 2019-04- 23 08:52:32“},inwardItem
= {” inward_item_id“:16,” inward_id“:2,” category_id“:1,” product_id“:1,” product_variant_id“:3,”条形码“: “ 99”,“数量”:12,“每件价格”:3,“消费者折扣率”:3,“消费者折扣率”:0,“卖方保证金率”:0:“卖方利润率”:0,“已删除”:0,“创建者”: “ 2019-04-23 08:46:56”,“ updated_at”:“ 2019-04-23 08:52:30”}其中stock_id
= 16)
StockModel.php
class StockModel extends Eloquent {
protected $table = 'stock';
protected $primaryKey = 'stock_id';
protected $fillable = ['inward_id', 'inward_item_id', 'category_id', 'product_id', 'product_variant_id', 'stock_qty', 'initial_qty', 'is_closed', 'is_deleted'];
public function fetchData($data) {
$stock_info = StockModel::where($data['condition']);
$stock_info = $stock_info->get();
if (!empty($stock_info) && isset($data['associate_relationships']) && $data['associate_relationships'] == TRUE) {
if ($data['type'] == 'OBJECT' || $data['type'] == 'OBJECT_FIRST') {
$this->associateRelationship($stock_info);
}
}
return $stock_info;
}
protected function associateRelationship($objects, $options = NULL) {
$new_object = $objects;
$has_multiple_objects = TRUE;
if (!isset($objects[0])) {
$new_object=[];
$new_object[0] = $objects;
$has_multiple_objects = FALSE;
}
foreach ($new_object as $key => $value) {
//get category data
$value->category = $value->category;
//get products data
$value->product = $value->product;
//get products variant data
$value->productVariant = $value->productVariant;
//get inward data
$value->inward = $value->inward;
//get inward item data
$value->inwardItem = $value->inwardItem;
}
// dd($new_object);
if ($has_multiple_objects == FALSE) {
$new_object = $new_object[0];
}
return $new_object;
}
public function inward() {
$inwardModel = new InwardModel();
return $this->hasOne($inwardModel, 'inward_id', 'inward_id');
}
public function inwardItem() {
$inwardItemModel = new InwardItemModel();
return $this->hasOne($inwardItemModel, 'inward_item_id', 'inward_item_id');
}
public function category() {
$categoryModel = new CategoryModel();
return $this->hasOne($categoryModel, 'category_id', 'category_id');
}
public function product() {
$productModel = new ProductModel();
return $this->hasOne($productModel, 'product_id', 'product_id');
}
public function productVariant() {
$productVariantModel = new ProductVariantModel();
return $this->hasOne($productVariantModel, 'product_variant_id', 'product_variant_id');
}
}
控制器
$q = [
'condition' => [
$inwardItemModel_tbl_name . '.barcode' => $search_query['barcode'],
$stockModel_tbl_name . '.is_closed' => 0,
$stockModel_tbl_name . '.is_deleted' => 0,
],
'joins' => [
'InwardItemModel' => [
'enable' => 1,
'condition' => [
$inwardItemModel_tbl_name . '.is_deleted' => 0
]
],
],
'type' => 'OBJECT_FIRST',
'associate_relationships' => TRUE,
];
$stock = $stockModel->fetchData($q);
if (!empty($stock) && $stock->stock_qty > 0) {
$qty = (float) $search_query['selected_qty'];
$qty = 1;
$stock->stock_qty-=$qty;
$stock->save(); #ERROR IS OCCURED HERE.
$info['success'] = TRUE;
$info['msg'] = 'Deducted successfully';
}
我知道即使我已指定category
StockModel
中的$fillables
字段视为一个字段
答案 0 :(得分:0)
出现此问题的原因是由于您如何“关联”模型的不同关系。您无需将Relationship属性分配回该对象。这实际上是由于Eloquent如何处理关系属性而导致的问题。您在这里实际要做的是将关系分配为模型的属性,以便以后保存时尝试将模型另存为字段。
我也强烈建议不要循环浏览模型来单独加载关系,因为这会导致多个查询和低效的代码。相反,请注意Eager Loading,因为它消除了n+1
问题,即分别为每个模型加载关系。
如果您使用紧急加载,则可以完全摆脱associateRelationship
,即
public function fetchData($data)
{
$stock_info = StockModel::where($data['condition']);
if (isset($data['associate_relationships']) && $data['associate_relationships']) {
if ($data['type'] == 'OBJECT' || $data['type'] == 'OBJECT_FIRST') {
$stock_info->with('category', 'product', 'productVariant', 'inward', 'inwardItem');
}
}
return $stock_info;
}
此外,仅供参考,您无需将模型的实例传递给关系方法,您可以使用::class
常量代替,例如:
public function inward()
{
return $this->hasOne(InwardModel::class, 'inward_id', 'inward_id');
}
最后,我也建议您查看:
Incrementing and decrement a field
Using whereHas(而不是联接)
Retrieving a single model(而不是收藏集)