Laravel Eloquent::Find() 使用现有 ID 返回 NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26139886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:13:28  来源:igfitidea点击:

Laravel Eloquent::Find() returning NULL with an existing ID

phplaravelormfindeloquent

提问by zedee

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

有一个模型叫做 Site

I'm using Eloquent ORM, so when I call (in a controller)

我正在使用 Eloquent ORM,所以当我调用(在控制器中)

$oSite = Site::find(1)

and then

进而

var_dump($oSite);

It returns a value of NULL.

它返回一个值NULL

But when I check the database, the table 'sites' actually contains the following item:

但是当我检查数据库时,表“sites”实际上包含以下项目:

id: 1
user_id: 1
name: test

In my SitemodelI have the following code:

在我的Site模型中,我有以下代码:

use Illuminate\Database\Eloquent\ModelNotFoundException;

 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?

我做错了什么?我没有得到文档的哪一部分?

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)

编辑2:(已解决)

Real reason why wasn't working:

不工作的真正原因:

I had originallyin 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 = ['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 ...).

问题是我deleted_at在启动项目后添加了一个“ ”列,当我应用迁移时,我没有启用软删除。显然,我犯了第二个错误,忘记使 ' deleted_at' 可以为空,因此所有插入都有错误的时间戳 (0000-00-00 ...)。

Fix:

使固定:

  1. Made nullable 'deleted_at' column.

  2. Set all wrong 'deleted_at' timestamps to NULL.

  1. 使“ deleted_at”列可以为空。

  2. 将所有错误的 ' deleted_at' 时间戳设置为NULL.

回答by Anand Patel

Check you are getting Input::get('siteId')correctly. if you are getting it then try to convert it into integer i.e

检查你是否Input::get('siteId')正确。如果你得到它然后尝试将它转换为整数即

$iSiteToDelete = intval(Input::get('siteId'));

回答by Jarek Tkaczyk

You're not returning your model.

你没有返回你的模型。

var_dumpprints output and returns nothing.

var_dump打印输出并且不返回任何内容。

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