2
Contributors
5
Replies
46
Views
3 Years
Discussion Span
Last Post by jaspertan
1

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

Edited by cereal

0

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

0

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

0

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
            )
        );
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.