問題描述
它非常簡單,因為它是最基本的東西,但我不知道我錯過了什么:
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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!