我使用laravel eloquent方法在db中插入了一行。请参阅下面的代码,
public function store() {
$language = new languages;
$language -> languages = Input::get('languages');
$language -> created_by = Auth::user()->id;
$language -> updated_by = Auth::user()->id;
if( $language -> save() ) {
$returnData = languages::where("id","=",$language -> id) -> get();
$data = array ("message" => 'Language added successfully',"data" => $returnData );
$response = Response::json($data,200);
return $response;
}
}
我想要从表中插入最后一行,但我的响应包含空数据。请指导获取数据的正确方法?
答案 0 :(得分:3)
几乎就是:您需要致电first()
才能获得一行。但是,既然你使用了雄辩的话,你可以调用find()
方法:
public function store() {
$language = new languages;
$language->languages = Input::get('languages');
$language->created_by = Auth::user()->id;
$language->updated_by = Auth::user()->id;
if($language->save()) {
$returnData = $language->find($language->id);
$data = array ("message" => 'Language added successfully',"data" => $returnData );
$response = Response::json($data,200);
return $response;
}
}