Avinash Kumar
Avinash Kumar

Reputation: 777

how to get current entry info in laravel eloquent

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)

Upvotes: 2

Views: 414

Answers (1)

The Alpha
The Alpha

Reputation: 146191

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

Upvotes: 1

Related Questions