Php 问题拉威尔关系
我有两个表Php 问题拉威尔关系,php,laravel,orm,Php,Laravel,Orm,我有两个表users和notes,我试图以某种方式与它们关联,但当我创建时,我的有许多和属于脚本不会加载并在一定时间后超时 我正在尝试创建一个基本的便笺添加系统,以便您可以将便笺添加到用户的帐户中。我已经成功地与帐户上有便笺的用户建立了hasMany关系,但是,我正在尝试在notes上定义一种关系,它通过创建便笺的用户来实现 我的数据库布局如下: 用户: `id`, `username`, `password` 1 , Connor , hash `id`, `note`, `us
users
和notes
,我试图以某种方式与它们关联,但当我创建时,我的有许多和属于脚本不会加载并在一定时间后超时
我正在尝试创建一个基本的便笺添加系统,以便您可以将便笺添加到用户的帐户中。我已经成功地与帐户上有便笺的用户建立了hasMany
关系,但是,我正在尝试在notes
上定义一种关系,它通过创建便笺的用户来实现
我的数据库布局如下:
用户:
`id`, `username`, `password`
1 , Connor , hash
`id`, `note`, `user_id`, `user_id_created`
1 , Hello , 1 , 1
注意事项:
`id`, `username`, `password`
1 , Connor , hash
`id`, `note`, `user_id`, `user_id_created`
1 , Hello , 1 , 1
这意味着user ID 1
已经创建了一个针对自己的注释,因此在我的用户模型中,我使用:
class User {
public function notes(){
return $this->hasMany("App\Note");
}
public function created_notes(){
return $this->hasMany("App\Note", "id", "user_id_created");
}
}
class Note {
protected $with = ['created_by']
public function created_by(){
return $this->belongsTo("App\User", "user_id_created", "id");
}
}
然后在我的Notes模型中,我使用:
class User {
public function notes(){
return $this->hasMany("App\Note");
}
public function created_notes(){
return $this->hasMany("App\Note", "id", "user_id_created");
}
}
class Note {
protected $with = ['created_by']
public function created_by(){
return $this->belongsTo("App\User", "user_id_created", "id");
}
}
但是,当我完成此操作并尝试在我的便笺模型上使用$with=['created_by']
时,脚本崩溃,根本无法加载
有人能解释一下吗
注释模型
用户模型
在控制器中
用户模型应该是
class User {
public function notes(){
return $this->hasMany(Note::class);
}
}
和注释模型
class Note {
public function user(){
return $this->belongsTo(User::class);
}
}
现在,只需从notes实例调用user(),即可获得创建notes的用户的详细信息。这就是您要查找的内容吗
用户模型:
class User extends Authenticatable
{
public function notes(){
return $this->hasMany("App\Note");
}
public function created_notes(){
return $this->hasMany("App\Note", "id", "user_id_created");
}
}
注:型号:
use Illuminate\Database\Eloquent\Model;
class Note extends Model {
protected $with = ['created_by'];
public function created_by()
{
return $this->belongsTo("App\User", "user_id_created", "id");
}
}
修补程序结果:
>>> $note = App\Note::find(1)
=> App\Note {#771
id: 1,
note: "1",
user_id: 1,
user_id_created: 1,
created_at: "2018-05-21 08:58:32",
updated_at: "2018-05-21 08:58:32",
created_by: App\User {#778
id: 1,
name: "bob",
email: "bob@example.com",
created_at: "2018-05-21 08:58:22",
updated_at: "2018-05-21 08:58:22",
},
}
请显示您的查询(即$with=['created_by'])您能详细说明一下您的意思吗,对不起?您能尝试将您的方法名称转换为驼峰大小写(即,更改为createdBy()
和createdNotes()
)并更改$with
参数以反映这一点吗?Laravel可能正在从snake case转换为渴望加载。粘贴整个query@benj这具有相同的效果(脚本拒绝加载)。这将如何合并创建便笺的用户–这将仅将便笺附加到与其相关的用户,而不会引入创建便笺的用户。请参见get_notes()
方法,我们使用了和('user')
这不是我想要的;我需要这个便笺来引入由列user\u id\u created创建它的用户。你能解释一下你到底想要什么吗?我想让人们使用note::find(1)->createdBy(),它引入与Notes表上创建的列user\u id\u对应的用户。出于某种原因,这根本没有加载页面。我以前做过,但它没有加载。拉威尔在呜呼声中抛出了一个最大60秒的加载错误!没有修补程序也不会做任何事情。通过修补程序使用时出现了“Symfony\Component\Debug\Exception\FatalErrorException:允许的内存大小为134217728字节(尝试分配1576960字节)”。我想问题在于我在用户表上使用$with=['notes']时。它一定在无限地重复自己。