Laravel将数组转换为模型

Gag*_*gan 14 laravel laravel-4

我有一个数组如下

      'topic' => 
  array (
    'id' => 13,
    'title' => 'Macros',
    'content' => '<p>Macros. This is the updated content.</p>
',
    'created_at' => '2014-02-28 18:36:55',
    'updated_at' => '2014-05-14 16:42:14',
    'category_id' => '5',
    'tags' => 'tags',
    'referUrl' => '',
    'user_id' => 3,
    'videoUrl' => '',
    'useDefaultVideoOverlay' => 'true',
    'positive' => 0,
    'negative' => 1,
    'context' => 'macros',
    'viewcount' => 60,
    'deleted_at' => NULL,
  )
Run Code Online (Sandbox Code Playgroud)

我想使用这个数组并将其转换/转换为主题模型.有没有办法可以做到这一点.

谢谢

Lau*_*nce 19

尝试创建一个新对象并将数组传递给构造函数

$topic = new Topic($array['topic']);
Run Code Online (Sandbox Code Playgroud)


Liq*_*aut 17

要从单个项目数组创建模型:

$Topic = new Topic();
$Topic->fill($array);
Run Code Online (Sandbox Code Playgroud)

要从项目数组创建集合:

$Topic::hydrate($result);
Run Code Online (Sandbox Code Playgroud)