gpt4 book ai didi

php - Laravel错误: Trying to get property of non-object

转载 作者:行者123 更新时间:2023-11-29 23:06:18 28 4
gpt4 key购买 nike

我正在使用 larvel 4.2 并收到错误:尝试获取非对象的属性

模型:User.php(这是错误行)

foreach(self::with('project')->where('user_id', '=', $user->id)->get() as $row)
{
}

模型:User.php(我也有这些)

namespace Project;

class User extends \Eloquent {

protected $table = 'projects_users';

/**********************************************************
* Methods to use with loaded User
**********************************************************/

/**
* @return User
*/
public function user()
{
return $this->belongsTo('User', 'user_id')->orderBy('firstname', 'ASC');
}

/**
* @return Project
*/
public function project()
{
return $this->belongsTo('Project', 'project_id')->orderBy('name', 'ASC');
}

不确定为什么会收到此错误?有什么想法吗?

最佳答案

http://php.net/manual/en/control-structures.foreach.php

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

您正在尝试迭代错误。您的查询返回一个 bool 值。

首先回显您的查询,看看它返回什么。

从你的模型来看,似乎没有什么问题。

关于php - Laravel错误: Trying to get property of non-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28284198/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com