我正在尝试从id <10的模型Customer返回客户。我在数据库中确实有一些客户。
$customers = Customer::where('id', '<', 10)->get();
return json_encode(['customers' => $customers]);
当我尝试dd($ customers)时,我得到了一个集合Customer 的列表
我不知道为什么我继续这样做以及为什么模型返回原始集合而不是客户列表的对象???
似乎空日期“updated_at”字段导致错误。不知道为什么!
在我必须做的客户模型上:
//ask Laravel to not manage default date columns
public $timestamps = false;
我也要改变那些列:
public function setDateRemindedAttribute($value)
{
if (strlen($value)) {
$this->attributes['date_reminded'] = Carbon::createFromFormat('d/m/Y', $value);
} else {
$this->attributes['date_reminded'] = null;
}
}
public function setUpdatedAtAttribute($value)
{
if (strlen($value)) {
$this->attributes['updated_at'] = Carbon::createFromFormat('d/m/Y', $value);
} else {
$this->attributes['updated_at'] = $_SERVER['REQUEST_TIME'];
}
}
像这样使用response()->json()
$customers = Customer::where('id', '<', 10)->get();
return response()->json(['customers' => $customers]);