2015-01-10 44 views
15

我正在使用「Laravel中的雄辯ORM」執行數據庫操作。我只想在數據庫中取最後一個插入ID(不是最大ID)。如何在Eloquent ORM laravel中獲取最後一個插入標識

我搜索了laravel的最後一個插入id雄辯的ORM,我得到了下面的鏈接(Laravel, get last insert id using Eloquent),它指的是從下面的函數「$data->save()」中獲取最後一個插入id。

但我需要得到

Model::create($data)」;

我的查詢:

GeneralSettingModel::create($GeneralData); 

User::create($loginuserdata); 

我怎樣才能獲取最後插入的ID?

+1

的可能的複製[Laravel,獲得使用雄辯最後插入ID(http://stackoverflow.com/問題/ 21084833/laravel-get-last-insert-id-using-eloquent) –

+0

如果'如果表有一個自動遞增的id'試試這個:https://stackoverflow.com/a/46482971/2815635 – C2486

回答

36

像文檔說:Insert, update, delete

「你也可以使用create方法來保存新模式單一 線插入的模型實例將從 方法退還給您。然而,在這樣做之前,你需要在模型上指定一個 可填寫或防護屬性,因爲所有的雄辯車型 防止大規模分配。

薩維後NG或創建一個使用自動遞增的ID的新模式, 您可以通過訪問對象的id屬性檢索ID:

$insertedId = $user->id; 

所以你的樣品中:

$user = User::create($loginuserdata); 
$insertedId = $user->id; 

然後對錶2也將是

$input['table2_id'] = $insertedId; 
table2::create($input); 
+2

值得注意的是,文檔是誤導性的,因爲第一個方法顯示爲' - > save();'它並沒有IMO足夠解釋'save()'返回一個布爾值while * only * Insert,update,並刪除返回實例。特別是因爲在「之後**保存**或創建」 – DelightedD0D

19

****對於Laravel ****

$user = new User(); 

$user->name = 'John'; 

$user->save(); 

//Getting Last inserted id 

$insertedId = $user->id; 
+0

它不適用於我:( –

+0

$ lastInsertedID = DB :: table('users') - > insert(array( 'email_id'=> $ email_id , 「名」 => $命名 ) ) - > lastInsertId(); –

+0

感謝的答案,但我解決我的問題,這是關於PrimaryKey的,我在模型改變primarkey所以我沒有得到最後的ID更改後。主鍵到ID,它的工作 –

3
$lastId = User::create($loginuserdata)->id; 
2

正如有人說bfore我,你可以通過使用

$model->id; 

檢索ID,但只有當你使用標準的命名約定侃侃而談。如果您想使用其他名稱的PrimaryKey列,如:

通過調用

$model->userid; 

它在描述

class Users extends Model{ 
    $primaryKey = 'userid'; 
} 

你可以檢索最後插入的ID:https://laravel.com/docs/master/eloquent#eloquent-model-conventions在那裏可以找到:

Eloquent也會假定每個表都有一個名爲id的主鍵列 。您可以定義一個$ primaryKey屬性來覆蓋這個約定。

另外,Eloquent假定主鍵是一個遞增的 整數值,這意味着默認情況下主鍵將自動轉換爲int到 。如果您希望使用非遞增或非數字主鍵,則必須將模型上的公共$增量 屬性設置爲false。

+0

親愛的@Krzysiek Grzembski我需要它在我的controller.I改變它在模型中,因爲你寫在模型中。但是現在$ model-> userid;是在哪裏accessible.I需要它在我的controller.Please指導... – Raham

+0

@Raham請看我更新的answear。沒有任何代碼很難說出什麼問題。 –

+0

其實我解決了我的問題。我的問題是,我如何檢索最後一個插入的表。現在我查找你的答案,你如何返回最後插入的一個。你定義$模型的地方... – Raham

3

你可以換你的數據在insertGetId()方法,這樣

$id = DB::table('table')->insertGetId($data); 

在這種情況下,數組$的數據會是這個樣子

$data = [ 'field' => 'data' , 'field' => 'data' ]; 

,如果你是使用數據庫外觀,您可以將lastInsertID()方法追加到查詢中。

$lastInsertedID = DB::table('table')->insert($data)->lastInsertId(); 
+0

值得注意的是,只有當列實際上被命名爲id時,insertGetId纔有效。 –

2

你可以做得一樣,

public function store(Request $request,ModelName $obj) 
{ 
     $lastInsertedId = $obj->create($request->all())->id; 

} 

希望這會幫助你。

0
$user = (new user)->create($request->all()) ; 

     \Session::flash('message', 'Thanks , Your record No (' .$user->id . ') has been Successfully added'); 

That`s我的方式來做到這一點,我回到了插入的對象,然後得到它的ID或其他財產..

容易..和完整

0

此代碼是偉大的工作對我來說: Laravel 5.3

$upstatus = User::create($status); return response()->json($upstatus);

用戶網頁/網站,我是CAL升data.id

0

雄辯型號

$user = new Reports();   
$user->email= '[email protected]'; 
$user->save(); 
$lastInsertId = $user->id; 

使用查詢生成器

$lastInsertId = DB::table('reports')->insertGetId(['email' => '[email protected]']); 
相關問題