Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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
使用';JSON';柱_Json_Database_Laravel 5_Eloquent_Php 7 - Fatal编程技术网

使用';JSON';柱

使用';JSON';柱,json,database,laravel-5,eloquent,php-7,Json,Database,Laravel 5,Eloquent,Php 7,在Laravel 5中,如果外键存在于JSON列的字段中,是否可以使用Eloquent创建关系 若有,原因为何 例如: 我有一个名为chats的表,其中有一个participantIds列,属于JSON数据类型。数据的JSON格式如下所示: {"creator": "1", "recipient": "2"} 我想使用这些字段加入users表,以获取聊天的参与者。 如何做到这一点?Laravel对JSON关系没有本机支持 我为此创建了一个包: 您可以这样使用它: {"creator": "1"

在Laravel 5中,如果外键存在于
JSON
列的字段中,是否可以使用Eloquent创建关系

若有,原因为何

例如: 我有一个名为
chats
的表,其中有一个
participantIds
列,属于
JSON
数据类型。数据的
JSON
格式如下所示:

{"creator": "1", "recipient": "2"}
我想使用这些字段加入
users
表,以获取聊天的参与者。
如何做到这一点?

Laravel对JSON关系没有本机支持

我为此创建了一个包:

您可以这样使用它:

{"creator": "1", "recipient": "2"}
类聊天扩展模型
{
使用\Staudenmeir\EloquentJsonRelations\hasjsonrelations;
受保护的$casts=[
'participantIds'=>'json',
];
公共函数创建者()
{
返回$this->belongsTo(User::class,'participantIds->creator');
}
公共职能接受者()
{
返回$this->belongsTo(用户::类,'participantIds->recipient');
}
}
类用户扩展模型
{
使用\Staudenmeir\EloquentJsonRelations\hasjsonrelations;
公共函数createdChats()
{
返回$this->hasMany(Chat::class,'participantIds->creator');
}
公共职能部门收到的通知()
{
返回$this->hasMany(Chat::class,'participantIds->recipient');
}
}

Laravel不支持JSON关系

我为此创建了一个包:

您可以这样使用它:

{"creator": "1", "recipient": "2"}
类聊天扩展模型
{
使用\Staudenmeir\EloquentJsonRelations\hasjsonrelations;
受保护的$casts=[
'participantIds'=>'json',
];
公共函数创建者()
{
返回$this->belongsTo(User::class,'participantIds->creator');
}
公共职能接受者()
{
返回$this->belongsTo(用户::类,'participantIds->recipient');
}
}
类用户扩展模型
{
使用\Staudenmeir\EloquentJsonRelations\hasjsonrelations;
公共函数createdChats()
{
返回$this->hasMany(Chat::class,'participantIds->creator');
}
公共职能部门收到的通知()
{
返回$this->hasMany(Chat::class,'participantIds->recipient');
}
}

我一整天都在努力使用这么好的软件包。但是我得到了以下错误:require php^7.2.5 | | ^8.0->您的php版本(7.2;通过config.platform重写,实际版本:7.2.5)不满足该要求。我花了一整天的时间才使用这么好的软件包。但是我得到了以下错误:require php^7.2.5 | | ^8.0->您的php版本(7.2;通过config.platform重写,实际:7.2.5)不满足该要求。