Laravel 4 - 返回当前插入的 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17139935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 - Return the id of the current insert
提问by BigJobbies
I have the following query
我有以下查询
public static function createConversation( $toUserId )
{
$now = date('Y-m-d H:i:s');
$currentId = Auth::user()->id;
$results = DB::table('pm_conversations')->insert(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
return $results;
}
How would i return the id of the row just inserted?
我将如何返回刚刚插入的行的 id?
Cheers,
干杯,
回答by Kylie
Instead of doing a raw query, why not create a model...
与其进行原始查询,不如创建一个模型……
Call it Conversation, or whatever...
称之为对话,或者其他什么......
And then you can just do....
然后你就可以做....
$result = Conversation::create(array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now ))->id;
Which will return an id...
哪个会返回一个id...
Or if you're using Laravel 4, you can use the insertGetId method...In Laravel 3 its insert_get_id() I believe
或者,如果您使用的是 Laravel 4,则可以使用 insertGetId 方法...在 Laravel 3 中,我相信它的 insert_get_id()
$results = DB::table('pm_conversations')->insertGetId(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
This method requires that the id of the table be auto-incrementing, so watch out for that...
此方法要求表的 id 自动递增,因此请注意...
The last method, is that you can just return the last inserted mysql object....
最后一个方法,就是你可以只返回最后插入的mysql对象....
Like so...
像这样...
$result = DB::connection('mysql')->pdo->lastInsertId();
So if you choose that last road...
所以如果你选择最后一条路...
It'll go...
它会去...
public static function createConversation( $toUserId )
{
$now = date('Y-m-d H:i:s');
$currentId = Auth::user()->id;
$results = DB::table('pm_conversations')->insert(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
$theid= DB::connection('mysql')->pdo->lastInsertId();
return $theid;
}
I would personally choose the first method of creating an actual model. That way you can actually have objects of the item in question. Then instead of creating a model and just save()....you calll YourModel::create() and that will return the id of the latest model creation
我个人会选择第一种创建实际模型的方法。这样,您实际上可以拥有相关项目的对象。然后,而不是创建一个模型,而只是 save().... 你调用 YourModel::create() ,这将返回最新模型创建的 id
回答by Chittaranjan
You can use DB::getPdo()->lastInsertId()
.
您可以使用DB::getPdo()->lastInsertId()
.
回答by kolexinfos
Using Eloquent you can do:
使用 Eloquent,您可以执行以下操作:
$new = Conversation();
$new->currentId = $currentId;
$new->toUserId = $toUserId;
$new->ip = Request::getClientIp();
$new->time = $now;
$new->save();
$the_id = $new->id; //the id of created row
回答by Artorias2718
The way I made it work was I ran an insert statement, then I returned the inserted row ID (This is from a self-learning project to for invoicing):
我让它工作的方式是我运行了一个插入语句,然后我返回了插入的行 ID(这是一个自学项目,用于开票):
WorkOrder::create(array(
'cust_id' => $c_id,
'date' => Input::get('date'),
'invoice' => Input::get('invoice'),
'qty' => Input::get('qty'),
'description' => Input::get('description'),
'unit_price' => Input::get('unit_price'),
'line_total' => Input::get('line_total'),
'notes' => Input::get('notes'),
'total' => Input::get('total')
));
$w_id = WorkOrder::where('cust_id', '=', $c_id)->pluck('w_order_id');
return $w_id;