如何在Laravel中返回当前插入的行数据?

时间:2014-07-07 06:37:39

标签: laravel laravel-4 eloquent

我使用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; 
    } 
}

我想要从表中插入最后一行,但我的响应包含空数据。请指导获取数据的正确方法?

1 个答案:

答案 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; 
    } 
}