BigJobbies
BigJobbies

Reputation: 4033

Laravel 4 - Return the id of the current insert

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,

Upvotes: 7

Views: 26116

Answers (4)

kolexinfos
kolexinfos

Reputation: 1002

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

Upvotes: 10

Artorias2718
Artorias2718

Reputation: 633

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;

Upvotes: 0

Chittaranjan
Chittaranjan

Reputation: 449

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

Upvotes: 13

Kylie
Kylie

Reputation: 11749

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

Upvotes: 13

Related Questions