Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Laravel HasManyThrough关系获取模型id和更新字段时出错_Php_Laravel_Model View Controller_Laravel 4_Eloquent - Fatal编程技术网

Php 使用Laravel HasManyThrough关系获取模型id和更新字段时出错

Php 使用Laravel HasManyThrough关系获取模型id和更新字段时出错,php,laravel,model-view-controller,laravel-4,eloquent,Php,Laravel,Model View Controller,Laravel 4,Eloquent,我已经创建了3个laravel模型,即组织、用户和联系人,它们之间的关系非常密切 我已经为模型定义了所有必需的关系 我必须更新属于某个组织的联系人的名为“deletedate”的特定字段 我使用了以下代码: 类ContactsController扩展BaseController { 函数_u构造() { $this->user=user::find(Auth::id()); $this->organization=$this->user->organization; } 公共功能更新_it()

我已经创建了3个laravel模型,即组织、用户和联系人,它们之间的关系非常密切

我已经为模型定义了所有必需的关系

我必须更新属于某个组织的联系人的名为“deletedate”的特定字段

我使用了以下代码:

类ContactsController扩展BaseController
{
函数_u构造()
{
$this->user=user::find(Auth::id());
$this->organization=$this->user->organization;
}
公共功能更新_it()
{
$contact=$this->organization->contact()->find(输入::get('id');
$contact->deletedate=Carbon\Carbon::now()->toDateTimeString();
打印$contact->name;
打印$contact->id;
$contact->save();
}
}
在函数update_it()中

提取数据时,将提取属于该特定输入“id”的联系人的所有字段,但字段“id”除外。 不是获取联系人的id,而是获取用户的id

print$contact->name
此代码返回属于输入id的联系人的确切姓名

打印$contact->id
,同时此代码返回用户的id

$contact->save()
此代码更新联系人的“deletedate”字段,其id等于用户id

三种模型的辩护如下:

组织模式:

类组织扩展了雄辩的
{       
受保护的$table='组织';
公共职能联系人()
{
返回$this->hasManyThrough('Contact','User','organization_id','User_id');
}
}
用户模型:

类用户扩展了雄辩的
{
受保护的$table='users';
公共职能联系人()
{
返回$this->hasMany('Contact');
}
公共职能组织()
{
返回$this->belongsTo('Organization');
}
}
联系方式:

类接触扩展了雄辩的
{
受保护的$table='contacts';
公共函数用户()
{
返回$this->belongsTo('User');
}
}
从中可以看到,调用
find
方法将从生成的查询中获取所有列。因此,在您的案例中,有两个表连接到了
联系人
用户
,用户表
id
覆盖了联系人
id

为了只获取联系人字段,只需在
find
方法中指定所需的列,如下所示:

$contact = $this->organization->contact()->find(Input::get('id'), ['contacts.*']);

然后您将只加载联系人数据,
id
将是正确的:)

请在问题中发布定义树模型的代码。@shaddy他已经更新了它谢谢我没有注意到。我们真的需要某种通知系统!伟大的这真的很有帮助:)Thanx 4帮助:)