Laravel 4 - Return the id of the current insert

柔情痞子 提交于 2019-12-09 17:21:14

问题


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?

Cheers,


回答1:


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...

Or if you're using Laravel 4, you can use the insertGetId method...In Laravel 3 its insert_get_id() I believe

 $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...

The last method, is that you can just return the last inserted mysql object....

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




回答2:


You can use DB::getPdo()->lastInsertId().




回答3:


Using Eloquent you can do:

$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



回答4:


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):

        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;


来源:https://stackoverflow.com/questions/17139935/laravel-4-return-the-id-of-the-current-insert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!