Laravel Eloquent::Find() returning NULL with an existing ID(Laravel Eloquent::Find() 返回具有现有 ID 的 NULL)
问题描述
这很简单,因为它是最基本的东西,但我不知道我错过了什么:
It's pretty straightforward as it's the most basic thing but I don't know what I'm missing:
拥有一个名为 Site
我正在使用 Eloquent ORM,所以当我调用(在控制器中)时
I'm using Eloquent ORM, so when I call (in a controller)
$oSite = Site::find(1)
然后
var_dump($oSite);
它返回NULL
的值.
但是当我检查数据库时,表'sites'实际上包含以下项目:
But when I check the database, the table 'sites' actually contains the following item:
id: 1
user_id: 1
name: test
在我的 Site
model 我有以下代码:
In my Site
model I have the following code:
use IlluminateDatabaseEloquentModelNotFoundException;
Class Site extends Eloquent {
protected $table = 'sites';
protected $fillable = ['user_id', 'name'];
}
相反,如果我使用以下内容收集项目:
Instead, if I gather the item with the following:
$oSite = DB::table('sites')
->where('id', 1)
->first();
它有效,我得到了正确的寄存器.
It works and I get the correct register.
我做错了什么?我没有得到文档的哪一部分?
What I'm doing wrong? Which part of the documentation I didn't get?
型号代码可以在上面查看.
Model code can be checked above.
控制器:
use IlluminateSupportFacadesRedirect;
class SiteManagementController extends BaseController {
...
public function deleteSite()
{
if (Request::ajax())
{
$iSiteToDelete = Input::get('siteId');
$oSite = Site::find($iSiteToDelete);
return var_dump($oSite);
}
else
{
return false;
}
}
}
编辑 2:(已解决)
无法正常工作的真正原因:
我的模型代码中最初如下:
use IlluminateDatabaseEloquentSoftDeletingTrait;
use IlluminateDatabaseEloquentModelNotFoundException;
Class Site extends Eloquent {
protected $table = 'sites';
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
protected $fillable = ['user_id', 'name'];
}
问题是我在启动项目后添加了一个deleted_at
"列,当我应用迁移时,我没有启用软删除.显然,我犯了第二个错误,忘记启用 'deleted_at
' 可以为空,因此所有插入的时间戳都错误(0000-00-00 ...).
Problem was I added a 'deleted_at
' column after I started the project and when I applied migrations, I didn't have softdeleting enabled.
Obviously, I did a second error, forgetting to enable 'deleted_at
' to be nullable, hence all inserts went had a wrong timestamp (0000-00-00 ...).
修复:
将
deleted_at
"列设为可为空.
将所有错误的deleted_at
"时间戳设置为NULL
.
Set all wrong 'deleted_at
' timestamps to NULL
.
推荐答案
检查您是否正确获取 Input::get('siteId')
.如果你得到它然后尝试将它转换为整数,即
Check you are getting Input::get('siteId')
correctly. if you are getting it then try to convert it into integer i.e
$iSiteToDelete = intval(Input::get('siteId'));
这篇关于Laravel Eloquent::Find() 返回具有现有 ID 的 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel Eloquent::Find() 返回具有现有 ID 的 NULL
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Laravel 仓库 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01