【问题标题】:how to get current entry info in laravel eloquent如何在 laravel eloquent 中获取当前条目信息
【发布时间】:2014-07-11 23:27:35
【问题描述】:

我在mysql中有一个表

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

用雄辩的方式

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

我想知道上述条目的ques_id 是什么。 $qinfo->ques_id 不工作。

雄辩:

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

表架构

mysql> desc 问题; +------------+------------------+------+-----+---- -----------------+----------------+ |领域 |类型 |空 |钥匙 |默认 |额外 | +------------+------------------+------+-----+---- -----------------+----------------+ |问题ID | int(10) 无符号 |否 |优先级 |空 |自动增量 | |问题标题 | varchar(200) |否 |统一 |空 | | | created_at |时间戳 |否 | | 0000-00-00 00:00:00 | | |更新时间 |时间戳 |否 | | 0000-00-00 00:00:00 | | +------------+------------------+------+-----+---- -----------------+----------------+ 一组 4 行(0.01 秒)

【问题讨论】:

  • 您的 id 列是 'id' - 还是您专门将其设置为 'ques_id'?
  • 专门设置为 ques_id ,因为表只允许一个 auto_increment 列。
  • “$qinfo->ques_id 不工作”是什么意思 - 你上面的代码对我有用?
  • $qinfo->ques_id 没有返回任何值,理想情况下它应该返回当前递增的值。
  • 您能否粘贴表格中的整行或表格的架构?

标签: laravel laravel-4


【解决方案1】:

确保您的 Question 模型是这样的:

class Question extends ELoquent {

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

    // ...

}

现在,如果您尝试这个(您已经尝试过),那么它应该可以工作:

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

新创建模型的ques_id 可以使用以下方法检索:

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

【讨论】:

  • 它确实(我做到了),如果它不起作用,那么你犯了任何错误,尝试找到它。
  • 我更新了细节,更改模型类后是否需要运行任何命令
  • 哦!这是一个错字,很高兴它起作用了。更新为K :-)
猜你喜欢
  • 2013-09-05
  • 2020-05-06
  • 2011-05-08
  • 2014-03-01
  • 1970-01-01
  • 2011-02-27
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多