1
我试图通过Laravel中的模型关系获取数据库数据。 我已经建立了一个模型,像这样:返回null的Laravel关系
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class Userskeywords extends Eloquent {
public function relatedKeywords()
{
return $this->hasOne('Keywords', 'id', 'keywordId');
}
}
?>
与其他模型只是一个普通的模型。在数据库中,他们是这样的:
关键词
UsersKeywords
然而,当我运行UsersKeywords::with('relatedKeywords')->get()
返回NULL
为related_keywords
。执行以下代码时会发生这种情况。我究竟做错了什么?
$keywords = Userskeywords::where('user', '=', $id)->get();
$keywords->load('relatedKeywords');
return Response::json($keywords);
这将引发SQL错误。我不认为这是正确的。 – user4992124
什么SQL错误? –
无法在表格中找到该列'keywordId'。它看着错误的桌子,所以原来没问题。 – user4992124