将Laravel Collection传递给Vue

问题描述 投票:0回答:1

我正在尝试从id <10的模型Customer返回客户。我在数据库中确实有一些客户。

$customers = Customer::where('id', '<', 10)->get();
return json_encode(['customers' => $customers]);

上面的代码将错误输出“尾随数据”Trailing data error

当我尝试dd($ customers)时,我得到了一个集合Customer collection customer dump的列表

我不知道为什么我继续这样做以及为什么模型返回原始集合而不是客户列表的对象???

似乎空日期“updated_at”字段导致错误。不知道为什么!

Solved By:

在我必须做的客户模型上:

//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'];
    }
}
json laravel laravel-5.5
1个回答
0
投票

像这样使用response()->json()

$customers = Customer::where('id', '<', 10)->get();
return response()->json(['customers' => $customers]);
© www.soinside.com 2019 - 2024. All rights reserved.