将集合转换为 Eloquent 对象
Convert a collection into an Eloquent object
目前我在 Laravel 5.2 中使用 Xero API。我想对这些数据使用 Eloquent 的强大功能。
实际上我可以恢复发票,甚至可以使用链接方法过滤它们,如下所示:
$invoices = XeroPrivate::load('Accounting\Invoice')
->where('Status', 'DRAFT')
->execute();
如果我做 var_dump
,我会得到这样的数据:
object(XeroPHP\Remote\Collection)[173]
public 0 =>
object(XeroPHP\Models\Accounting\Invoice)[171]
protected '_data' =>
array (size=31)
'Type' => string 'ACCPAY' (length=6)
'Contact' =>
Eloquent 链接方法可以让我执行这样的事情。目前它失败了:
$invoices = XeroPrivate::load('Accounting\Invoice')
->where('Date','>','2016-03-20')
->execute();
检查Laravel的文档,我想我可以用collect
转换成一个集合:
$collection = collect($invoices);
$collection
没有解决问题。现在数据结构不同了但还是不能用Eloquent。现在数据是:
object(Illuminate\Support\Collection)[163]
protected 'items' =>
array (size=24)
0 =>
object(XeroPHP\Models\Accounting\Invoice)[171]
protected '_data' =>
array (size=31)
但显示的数据是 Illuminate\Support\Collection
并且似乎是正确的。
谢谢!
您可以使用 first()
方法获取 collection 的单个项目。
$entity = $collection->first();
You can find more information here关于Illuminate\Support\Collection
.
有哪些方法可用
目前我在 Laravel 5.2 中使用 Xero API。我想对这些数据使用 Eloquent 的强大功能。
实际上我可以恢复发票,甚至可以使用链接方法过滤它们,如下所示:
$invoices = XeroPrivate::load('Accounting\Invoice')
->where('Status', 'DRAFT')
->execute();
如果我做 var_dump
,我会得到这样的数据:
object(XeroPHP\Remote\Collection)[173]
public 0 =>
object(XeroPHP\Models\Accounting\Invoice)[171]
protected '_data' =>
array (size=31)
'Type' => string 'ACCPAY' (length=6)
'Contact' =>
Eloquent 链接方法可以让我执行这样的事情。目前它失败了:
$invoices = XeroPrivate::load('Accounting\Invoice')
->where('Date','>','2016-03-20')
->execute();
检查Laravel的文档,我想我可以用collect
转换成一个集合:
$collection = collect($invoices);
$collection
没有解决问题。现在数据结构不同了但还是不能用Eloquent。现在数据是:
object(Illuminate\Support\Collection)[163]
protected 'items' =>
array (size=24)
0 =>
object(XeroPHP\Models\Accounting\Invoice)[171]
protected '_data' =>
array (size=31)
但显示的数据是 Illuminate\Support\Collection
并且似乎是正确的。
谢谢!
您可以使用 first()
方法获取 collection 的单个项目。
$entity = $collection->first();
You can find more information here关于Illuminate\Support\Collection
.