【发布时间】:2020-04-05 17:34:15
【问题描述】:
在关系方法中使用 withDefault() 会导致服务器错误问题。为什么会这样?有什么原因吗?我相信我在语法上是正确的。
class Product extends Model
{
protected $table = 'products';
public function category() {
return $this->hasOne('App\Model\Category', 'id', 'category_id');
}
public function images() {
return $this->hasMany('App\Model\ProductImage', 'product_id', 'id')->withDefault();
}
}
如果不使用 withDefault() 方法,页面可以正常运行并显示图像,但使用此方法会触发服务器错误。
【问题讨论】:
-
一个 500 会伴随一个错误。检查您的 error_logs 或更改设置以显示所有错误。
-
那么,错误说明了什么?
-
代码图像不是实际代码。请使用您正在使用的实际代码编辑您的问题。使人们更容易复制/粘贴以尝试复制问题的东西
-
[2019-12-12 11:05:25] local.ERROR:调用模型 [App\Model\Product] 上的未定义关系 [images]。 {"exception":"[object] (Illuminate\\Database\\Eloquent\\RelationNotFoundException(code: 0): 调用模型 [App\\Model\\Product] 上未定义的关系 [images]。在 C:\\ xampp\\htdocs\\theliquorshop\\tls_api\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\RelationNotFoundException.php:34)
-
关系在没有 withDefaults() 方法的情况下工作正常
标签: php laravel eloquent laravel-6