Php Laravel 5雄辩的关系“;有很多“通过”;SQL异常
你好。昨天,我花了6个多小时来了解ORM关系在laravel 5中是如何工作的。但我对他们还不是很熟悉 经过百次错误、测试、研究等。。。我让我的一部分关系正常运作Php Laravel 5雄辩的关系“;有很多“通过”;SQL异常,php,mysql,sql,laravel,relation,Php,Mysql,Sql,Laravel,Relation,你好。昨天,我花了6个多小时来了解ORM关系在laravel 5中是如何工作的。但我对他们还不是很熟悉 经过百次错误、测试、研究等。。。我让我的一部分关系正常运作 模型之间的关系: 在控制器中工作的内容: 发生异常的位置 例外情况: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select `tasks`.*, `lists`.`u
模型之间的关系:
在控制器中工作的内容: 发生异常的位置
例外情况:
SQLSTATE[23000]:
Integrity constraint violation: 1052
Column 'id' in where clause is ambiguous
(SQL: select `tasks`.*, `lists`.`user_id` from `tasks`
inner join `lists` on `lists`.`id` = `tasks`.`list_id` where
`tasks`.`deleted_at` is null and `lists`.`deleted_at` is null
and `lists`.`user_id` = 1 and `id` = 5 limit 1)
/* The problem in where clause ^^^^^^^ */
由laravel生成的SQL查询不起作用。我认为必须定义
id
表
注意:类使用受保护的$table='lists'。列表
单词在PHP(T_List)中是本机的。这张桌子的名字改变了我的病假。Laravel关于“ORM自定义键”的文档有点小
谢谢您的时间。问题实际上是在
QueryBuilder
中
$request->user()->tasks()
返回QueryBuilder
我不确定它到底是如何工作的。但是我认为
where()
函数增加了额外的SQL
解决方案:
我改变了这一行:
$task = $request->user()->tasks()->where('id', '5')->first();
致:
其中,
tasks
是数据库中的表,id
是列。现在太迟了,但是如果您知道记录的id
,也可以使用find()
。
/* Next line throws the SQL Exception */
$task = $request->user()->tasks()->where('id', '5')->first();
SQLSTATE[23000]:
Integrity constraint violation: 1052
Column 'id' in where clause is ambiguous
(SQL: select `tasks`.*, `lists`.`user_id` from `tasks`
inner join `lists` on `lists`.`id` = `tasks`.`list_id` where
`tasks`.`deleted_at` is null and `lists`.`deleted_at` is null
and `lists`.`user_id` = 1 and `id` = 5 limit 1)
/* The problem in where clause ^^^^^^^ */
$task = $request->user()->tasks()->where('id', '5')->first();
$task = $request->user()->tasks()->where('tasks.id', '5')->first();