简体   繁体   English

如何从集合创建 Laravel Eloquent 模型?

[英]How to create a Laravel Eloquent Model from Collection?

In order to use Algolia search with Airtable data, in a Laravel application, I think need to create an Eloquent model with the data I have retrieved, to then make use of Laravel Scout.为了将 Algolia 搜索与 Airtable 数据一起使用,在 Laravel 应用程序中,我认为需要使用我检索到的数据创建一个 Eloquent 模型,然后才能使用 Laravel Scout。

Can I use Collection data, that I have retrieved from Airtable, as a Model without a DB?我可以使用从 Airtable 检索到的集合数据作为没有数据库的模型吗?

Thanks.谢谢。

there is a hydrate method for this:有一个hydrate方法:

YourModel::hydrate($collections->toArray())

But if you have only one object, you can just但是如果你只有一个对象,你可以

$res = new YourModel($your_object)

Or you can even use fill或者你甚至可以使用fill

(new YourModel())->fill($your_obj)

And obviously until you don't call like save or update or something like this, where laravel tries to save that model on the DB, you're safe (maybe consider "disabling" them from the model, so that if you accidentally call those methods, nothing happens)很明显,除非你不调用saveupdate或类似的东西,laravel 尝试将该模型保存在数据库上,否则你是安全的(也许可以考虑从模型中“禁用”它们,这样如果你不小心调用了那些方法,没有任何反应)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM