【问题标题】:Laravel exception: Creating default object from empty valueLaravel 异常:从空值创建默认对象
【发布时间】:2020-08-13 23:23:01
【问题描述】:

我只是通过实例化一个模型并设置一个属性来得到这个错误。

$order_datail = new OrderDetail;
$order_detail->quantity = $product['quantity'];

我正在寻找这个问题的原因,但找不到。

OrderDetail 的构造函数被执行。表是 order_details,但即使设置受保护的 $table = 'order_details',我仍然会收到此错误。是的,此表中有一个“数量”列。

奇怪的是,我对其他模型没有任何问题。

$order = new Order;
$order->pickup_date = $request['date'];
$order->pickup_time = $request['pickupTime'];

以上代码运行良好。

型号订单详情:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class OrderDetail extends Model
{
use SoftDeletes;

public function replaceds()
{
    return $this->hasMany('App\Models\Replaced');
}
public function orders()
{
    return $this->belongsTo('App\Models\Order');
}
public function products()
{
    return $this->hasMany('App\Models\Product');
}
public function collis()
{
    return $this->hasMany('App\Models\Colli');
}
}

有谁知道这可能是什么原因?

【问题讨论】:

  • 你能展示你的模型吗?
  • 抱歉,更新了问题
  • 请更正您在 $order_datail = new OrderDetail 中的拼写;它应该是“order_detail”
  • 你不能是认真的......检查了很多次。捂脸!

标签: laravel eloquent


【解决方案1】:

$order_datail = new OrderDetail; 存在拼写错误 改成:$order_detail = new OrderDetail;

【讨论】:

  • 你不能是认真的......检查了很多次。捂脸!
猜你喜欢
  • 2017-11-29
  • 2018-07-18
  • 2019-03-14
  • 1970-01-01
  • 2021-05-22
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多