2
votes

i have a table in mysql

ques_id (auto increment , primary) , 
ques_title (string)

By using eloquent

$qinfo = new Question ; 
$qinfo->ques_title = "xyz" ;
$qinfo ->save() ;

i want to know what will be ques_id of the above entry . $qinfo->ques_id is not working .

Eloquent :

class Question extends Eloquent {
    protected $table = 'questions';
    public $timestamps = false ;
    protected $primarykey = 'ques_id' ;
}

table schema

mysql> desc questions ;
+------------+------------------+------+-----+---------------------+----------------+

| Field      | Type             | Null | Key | Default             | Extra          |
+------------+------------------+------+-----+---------------------+----------------+
| ques_id    | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| ques_title | varchar(200)     | NO   | UNI | NULL                |                |
| created_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+------------+------------------+------+-----+---------------------+----------------+
4 rows in set (0.01 sec)
1
Is your id column 'id' - or have you specifically set it to 'ques_id'?Laurence
specifically set to ques_id ,as table allows only one auto_increment column .Avinash Kumar
What do you mean "$qinfo->ques_id is not working" - your code above works for me?Laurence
$qinfo->ques_id is not returning any value , ideally it should return current incremented value .Avinash Kumar
Can you please paste the full row from your table or the schema of the table?Rifat

1 Answers

1
votes

Make sure your Question model is something like this:

class Question extends ELoquent {

    //public $timestamps = false;
    protected $table = 'questions';
    protected $primaryKey = 'ques_id';

    // ...

}

Now if you try this (that you have already tried) then it should work:

$qinfo = new Question; 
$qinfo->ques_title = "xyz";
$qinfo ->save();

The ques_id of the newly created model could be retrieved using this:

dd($qinfo->ques_id); // an integer