【问题标题】:Function contains() on a non-object Laravel非对象 Laravel 上的函数 contains()
【发布时间】:2013-12-16 07:21:26
【问题描述】:
 $profiles = UserProfile::find(1)->user_id;
 if ($profiles->contains($uid)){
     // ...
 }

错误: Call to a member function contains() on a non-object.

知道我做错了什么吗?我正在尝试检查用户是否已经在表格中有一行。

【问题讨论】:

  • 您确定UserProfile::find(1)->user_id 会返回您期望的结果吗?这似乎暗示$profiles 是一个非对象,如果找不到用户,则可能是FALSENULL
  • 尝试检查 gettype($profiles) 并查看 $profiles 是否为对象

标签: php html laravel laravel-4


【解决方案1】:

这是因为$profiles 不是一个对象,因为你已经使用了

// returns "user_id" from the collection
$profiles = UserProfile::find(1)->user_id;

所以,$profiles 包含字段 user_id 的值,而不是 UserProfile 对象。如果你使用

$profiles = UserProfile::find(1);

如果表中有 id1 可用,它将返回 UserProfile 对象。所以,你可以使用

if ( $profiles->contains($uid) ) {
    //
}

【讨论】:

    【解决方案2】:

    我认为 UserProfile::find(1)->user_id 返回一个 numberstring 而不是 object 所以$profiles->contains($uid) 不适合你。

    【讨论】:

    • 它返回一个字符串,嗯还有其他选择吗?
    • 据我所知 UserProfile::find(1) 会为你返回一个对象
    • 如果我这样做,我会调用未定义的方法 Illuminate\Database\Query\Builder::contains() ,模型有问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多