Laravel Eloquent :: Find()返回NULL与现有的ID [英] Laravel Eloquent::Find() returning NULL with an existing ID
问题描述
这是非常简单的,因为它是最基本的东西,但我不知道我错过了什么:
拥有一个名为的模型
我使用了雄辩的ORM,所以当我打电话(在控制器中)
$ oSite = Site :: find(1)
然后
var_dump($ oSite);
它返回一个值 NULL
/ p>
但是当我查看数据库时,sites表实际上包含以下项目:
id:1
user_id:1
name:test
在我的网站
模型中,我有以下代码:
使用Illuminate\Database\Eloquent\ModelNotFoundException;
类网站扩展Eloquent {
protected $ table ='sites';
protected $ fillable = array('user_id','name');
}
相反,如果我收集以下项目:
$ oSite = DB :: table('sites')
- >其中('id',1)
- >首先();
它工作,我得到正确的注册。
我在做错什么?哪一部分我没有得到?
编辑:
控制器:
code>使用照明\Support\Facades\Redirect;
class SiteManagementController扩展BaseController {
...
public function deleteSite()
{
if(Request :: ajax() )
{
$ iSiteToDelete = Input :: get('siteId');
$ oSite = Site :: find($ iSiteToDelete);
return var_dump($ oSite);
}
else
{
return false;
}
}
}
编辑2: (SOLVED)
为什么不能正常工作的真正原因:
我在我的模型代码中有原来如下:
use Illuminate\Database\ Eloquent\SoftDeletingTrait;
使用Illuminate\Database\Eloquent\ModelNotFoundException;
类网站扩展Eloquent {
protected $ table ='sites';
使用SoftDeletingTrait;
protected $ dates = ['deleted_at'];
protected $ fillable = array('user_id','name');
}
问题是我添加了一个' deleted_at
'列,我启动项目后,当我应用迁移时,我没有启用软删除。
显然,我做了第二个错误,忘记启用'$ code> deleted_at '可以为空,因此所有插入都有错误的时间戳(0000-00-00)。
修正:
-
code> deleted_at '列。
-
设置所有错误'
deleted_at
'时间戳到NULL
。
您没有返回模型。
var_dump
打印输出并且不返回任何内容。
这样做:
dd($ oSite); //代表var_dump和die - 一个帮助方法
甚至更好,只需返回模型: / p>
return $ oSite; //将被转换为JSON字符串
It's pretty straightforward as it's the most basic thing but I don't know what I'm missing:
Having a model called Site
I'm using Eloquent ORM, so when I call (in a controller)
$oSite = Site::find(1)
and then
var_dump($oSite);
It returns a value of NULL
.
But when I check the database, the table 'sites' actually contains the following item:
id: 1
user_id: 1
name: test
In my Site
model I have the following code:
use Illuminate\Database\Eloquent\ModelNotFoundException;
Class Site extends Eloquent {
protected $table = 'sites';
protected $fillable = array ('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 documentantion I didn't get?
EDIT:
Model code can be checked above.
Controller:
use Illuminate\Support\Facades\Redirect;
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;
}
}
}
EDIT 2: (SOLVED)
Real reason why wasn't working:
I had originally in my model code the following:
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Database\Eloquent\ModelNotFoundException;
Class Site extends Eloquent {
protected $table = 'sites';
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
protected $fillable = array('user_id', 'name');
}
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 ...).
Fix:
Made nullable '
deleted_at
' column.Set all wrong '
deleted_at
' timestamps toNULL
.
You're not returning your model.
var_dump
prints output and returns nothing.
do this instead:
dd($oSite); // stands for var_dump and die - a helper method
and even better, simply return the model:
return $oSite; // will be cast to JSON string
这篇关于Laravel Eloquent :: Find()返回NULL与现有的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!