Laravel雄辩地返回空

时间:2018-10-30 04:52:11

标签: laravel eloquent

这是我的模特

namespace App\Models\Invitation;

use App\Models\Model;

class SendtoType extends Model
{
}

当我从SendtoType获取数据时,它返回空或null,但我确定sendto_types表中包含数据

    $types = SendtoType::all();
    dd($types); // return empty collection

    $types = SendtoType::find(1); 
    dd($types);// null

这是在我运行php artisan命令:reset_table sendto_types命令之后发生的。

我清除了缓存,但无法正常工作。

4 个答案:

答案 0 :(得分:1)

谢谢大家,我发现了我的错误:

我为表的delete_at列设置了时间戳记,但是忘记设置不为null,因此当我添加数据时它也填充了delete_at列。 这就是为什么雄辩的子句返回空的原因。 如此愚蠢的错误!

答案 1 :(得分:0)

您可以在模型类中使用实际表名在数据库中添加protected $table = 'sendto_types';

class SendtoType extends Model
{
   protected $table = 'sendto_types';
}

答案 2 :(得分:0)

命令command:reset_table不是内置命令,因此您需要查看源代码以查看其功能,但是根据其名称,我假设该命令清空了您的表。< / p>

在这种情况下,您的表为空,因此您的查询将不返回任何结果。

答案 3 :(得分:0)

使用查询生成器在laravel中获取数据时。您应该使用get()first()...

  $types = SendtoType::find(1)->get();
相关问题