【发布时间】:2019-12-22 19:57:42
【问题描述】:
首先为标题道歉,我想不出更好的描述。 我有一个控制器,它假设读取数据库然后将其作为对象返回。这个数据库最近经历了结构上的变化,我重新植入了它。我的控制器代码是:
$books = \App\book::where('userId',1)->get();
foreach ($books as $b) {
echo$b->bookId;
}
这将导致以下输出:
70008000
。
但是,如果我通过修补程序运行相同的程序,我会得到完全不同的结果。修补程序代码:App\Book::where('userId',1)->get();
将产生几条记录,例如:
Illuminate\Database\Eloquent\Collection {#3018 全部: [ 应用\图书{#2996 记录ID:1, bookId: "7iraCwAAQBAJ", 用户ID:1, 作者ID:1, 标题:“石灰街的女巫”, isbn:“9780307451064”, 描述:“1924 年,一位波士顿外科医生的妻子来体现了全国对招魂术的激烈辩论,这是一场运动 致力于与死者交流。记者称她为 石灰街的金发女巫,但她的追随者只知道她 作为玛格丽。她最直言不讳的拥护者是亚瑟柯南道尔爵士,他 他非常相信玛格丽的力量,以至于他敦促她进入 由《科学美国人》赞助的有争议的竞赛。她 超自然的礼物迷惑了四位法官。只有一个 留下来说服……著名的逃脱艺术家哈里·胡迪尼。贾赫 捕捉他们的电动公共竞争和竞争 将它们带入彼此的轨道。", 预览:"http://books.google.com/books?id=7iraCwAAQBAJ&dq=9780307451071&hl=&source=gbs_api", 封面:“http://books.google.com/books/content?id=7iraCwAAQBAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api”, book_status: "正在阅读", created_at: "2019-08-16 12:30:27", 更新时间:“2019-08-16 12:30:27”, },
在结果中,您会看到我突出显示了 bookId。当我通过控制器运行它时,这就是我期望返回的结果。
编辑
数据库
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->bigIncrements('recordId');
$table->string('bookId');
$table->integer('userId');
$table->integer('authorId');
//$table->integer('genreId');
$table->string('title');
$table->string('isbn');
$table->binary('description');
$table->string('preview');
$table->string('cover');
$table->string('book_status');
$table->timestamps();
});
//DB::statement('ALTER TABLE books AUTO_INCREMENT = 2000');
}
雄辩的模型
class book extends Model
{
protected $primaryKey = 'bookId';
protected $fillable = [
'bookId',
'userId',
'authorId',
'title',
'isbn',
'description',
'preview',
'cover',
'book_status'
];
//$guarded = [];
public function authors(){
return $this->belongsTo('App\author','authorId');
}
}
【问题讨论】:
-
您是否尝试清除缓存?
标签: php mysql laravel eloquent