Mike Thrussell
Mike Thrussell

Reputation: 4515

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.

Can I use Collection data, that I have retrieved from Airtable, as a Model without a DB?

Upvotes: 0

Views: 1307

Answers (1)

Alberto
Alberto

Reputation: 12899

there is a hydrate method for this:

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

(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)

Upvotes: 3

Related Questions