hi to all,

can anyone help me how to use select_last_id on laravel.

Thanks,

Dani AI

Generated

As suggested, the simplest reliable pattern is to let MySQL generate the AUTO_INCREMENT value, capture that returned id, and use it when inserting the related row. In the code shown the insert for personnel included 'personnel_id' explicitly (via Request::getClientPersonnelId()), which defeats the purpose of an AUTO_INCREMENT column. Two practical, safe steps are: (1) map the model so Eloquent knows the custom primary key, and (2) create the personnel row first, capture its key, then insert the orgs row inside a transaction so both inserts succeed or both roll back.

class Personnel extends Eloquent {
    protected $table = 'personnel';
    protected $primaryKey = 'personnel_id';
    public $timestamps = false; // set true only if created_at/updated_at exist
    protected $fillable = array(
        'hdo_id','first_name','middle_name','last_name','birth_date',
        'mobile_number','landline_number','gender',
        'provincial_address_1','provincial_address_2','provincial_address_3',
        'current_address_1','current_address_2','current_address_3'
    );
}
DB::transaction(function() use ($inputData, $orgName, $orgDate) {
    $person = Personnel::create($inputData);
    Org::create(array(
        'personnel_id'  => $person->getKey(), // primary key returned by Eloquent
        'orgs_name'     => $orgName,
        'date_attended' => $orgDate
    ));
});

Notes and cautions: if Query Builder is preferred, assign the return value of insertGetId() to a variable and use that for the second insert (as indicated). Do not pass personnel_id in the personnel insert when it is auto-incrementing. Consider adding an InnoDB foreign key on orgs.personnel_id referencing personnel(personnel_id) to enforce integrity, and use transactions for atomicity. Also prefer varchar for phone fields (to preserve leading zeros, plus signs and formatting) and set $timestamps = false on the model if the table has no timestamp columns.

Recommended Answers

All 5 Replies

Are you using Eloquent? If yes, right after you save something call the id property, for example:

$post        = new Post;
$post->title = 'Title';
$post->body  = 'Bla bla bla...';
$post->save();

$post->id; // <- last inserted id

Otherwise use the insertGetId() method:

$id = DB::table('users')->insertGetId(
    array('email' => 'john@example.com', 'votes' => 0)
);

As suggested here: http://laravel.com/docs/queries#inserts

here is my schema do i need to array all of this ?
i want also to adapt personal id to other table so i can join tables.

CREATE TABLE personnel (
personnel_id int(11) NOT NULL AUTO_INCREMENT,
hdo_id int(11) DEFAULT NULL,
first_name varchar(255) NOT NULL,
middle_name varchar(255) NOT NULL,
last_name varchar(255) DEFAULT NULL,
birth_date date NOT NULL,
mobile_number int(11) NOT NULL,
landline_number int(11) NOT NULL,
gender varchar(45) NOT NULL,
provincial_address_1 varchar(255) NOT NULL,
provincial_address_2 varchar(255) NOT NULL,
provincial_address_3 varchar(255) NOT NULL,
current_address_1 varchar(255) NOT NULL,
current_address_2 varchar(255) NOT NULL,
current_address_3 varchar(255) DEFAULT NULL,
PRIMARY KEY (personnel_id)
) ENGINE=InnoDB AUTO_INCREMENT=105 DEFAULT CHARSET=latin1

If you use the method insertGetId(), Laravel will return correctly, basing on the value of the auto_increment column. Docs:

By using Eloquent, instead, change the column name, for example:

$p               = new Personnel;
$p->first_name   = '...';
$p->middle_name  = '...';
$p->last_name    = '...';
$p->save();
$p->personnel_id; // <- last inserted id

It should work fine.

here is my schema do i need to array all of this ?

Sorry, I do not understand. :)

here is my code to add

all i want is to inherit the primary key to other table in order for me to join them

DB::table('personnel')->insertGetId(
            array(
                'first_name'    =>  $first_name, 
                    'last_name'     =>  $last_name,
                'middle_name'   =>  $middle_name,
                'birth_date'    =>  $birth_date,
                'gender'        =>  $gender,
                'mobile_number' => $mobile_number,
                'hdo_id'        =>$hdo_id,
                'landline_number' => $landline_number,
                'provincial_address_1'=> $provincial_address1,
                'provincial_address_2'=> $provincial_address2,
                'provincial_address_3'=>$provincial_address3, 
                'current_address_1'=> $current_address1,
                'current_address_2' => $current_address2,
                'current_address_3' => $current_address3,
                 'personnel_id'=> Request::getClientPersonnelId(),

            )
        );



DB::table('orgs')->insert(array(
            'personnel_id' = \\i need to insert the value from personnel table
            'orgs_name' => $org_name,
            'date_attended'=>$org_date_attended
            )
        );

i already resolve the issue thanks for help:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.