我正在上传图像并返回包含图像数据(例如宽度、大小等)的 json 数组。
$json = ImageUpload::handle(Input::file('filedata'));
if ($json !== false) {
$this->imageModel->create($this->user, $json);
return Response::json($json, 200);
}
现在这个创建方法只是通过 laravel eloquent 模型将新数据插入数据库。我想不出一种方法,插入数据,返回刚刚插入的数据的 id 并以某种方式合并 json 数组并将其与其他 json 数据一起返回?希望这是有道理的。
最佳答案
你做不到吗?
$json = ImageUpload::handle(Input::file('filedata'));
if ($json !== false)
{
$image = $this->imageModel->create($this->user, $json);
$json = ['image_id' => $image->id, 'upload' => $json];
return Response::json($json, 200);
}
编辑:
还有,这个方法是什么?
$this->imageModel->create($this->user, $json);
Eloquent create() 或存储库或其他东西?如果这是 Eloquent,那么您可能在此方法中遇到问题,因为 create 接受数组作为参数,它应该是:
$this->imageModel->create(['user' => $user, 'json' => $json]);
关于php - laravel4 获取模型外部的最后一个插入 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25693948/