Laravel 5 拉威尔模型的非对象性

Laravel 5 拉威尔模型的非对象性,laravel-5,eloquent,models,relationships,Laravel 5,Eloquent,Models,Relationships,我有一张餐桌 CREATE TABLE `people` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `job_title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `profile_pic` varchar(255) COLLATE utf8mb4_un

我有一张餐桌

CREATE TABLE `people` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `job_title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `profile_pic` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `departments` varchar(500) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=95 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
部门的桌子

CREATE TABLE `departments` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `people_id` int(11) NOT NULL,
  `department_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  • 一个人可以在多个部门工作
  • 在departments表中,外键是people\u id
在我的人物模型中

public function departments()
    {
 return $this->hasMany('App\Models\Departments', 'id', 'people_id');

    }
最后,我尝试返回一些结果

 return $people::find(1)->departments;
我得到了错误

**ErrorException in web.php line 129:
Trying to get property of non-object**

如果这是一个愚蠢的错误,请原谅,我已经使用laravel一段时间了,但以前从未使用关系建立模型。

尝试以下代码:-

Get All records
$people = People::with('departments')->get();
$people = json_decode(json_encode($people),true) //Convert in to array
echo "<pre>"; print_r($people); die; //print it

Get 1 record
$people = People::with('departments')->first();
$people = json_decode(json_encode($people),true) //Convert in to array
echo "<pre>"; print_r($people); die; //print it
获取所有记录
$people=people::with('departments')->get();
$people=json\u decode(json\u encode($people),true)//转换为数组
回声“;印刷(人);死亡//打印出来
获得1条记录
$people=people::with('departments')->first();
$people=json\u decode(json\u encode($people),true)//转换为数组
回声“;印刷(人);死亡//打印出来

希望有帮助

试试这段代码:-

Get All records
$people = People::with('departments')->get();
$people = json_decode(json_encode($people),true) //Convert in to array
echo "<pre>"; print_r($people); die; //print it

Get 1 record
$people = People::with('departments')->first();
$people = json_decode(json_encode($people),true) //Convert in to array
echo "<pre>"; print_r($people); die; //print it
获取所有记录
$people=people::with('departments')->get();
$people=json\u decode(json\u encode($people),true)//转换为数组
回声“;印刷(人);死亡//打印出来
获得1条记录
$people=people::with('departments')->first();
$people=json\u decode(json\u encode($people),true)//转换为数组
回声“;印刷(人);死亡//打印出来

希望有帮助

所以最后这是一个愚蠢的问题


Find接受id,但id 1不存在

所以最后这是一个愚蠢的问题


Find接受id,但id 1不存在

你确定你的模型课叫departments吗?通常是单数的。所以应该是:
$this->hasMany('App\Models\Department')
;不,是departmentsTry:
return$this->hasMany('App\Models\Departments','people\u id','id')$this->hasMany('App\Models\Department')
;不,是departmentsTry:
return$this->hasMany('App\Models\Departments','people\u id','id')