Join 如何在laravel中获取外键表的值
嗨,我正在尝试获取一个引用了两个表的表的值。我的桌子看起来像这样Join 如何在laravel中获取外键表的值,join,laravel-4,foreign-keys,Join,Laravel 4,Foreign Keys,嗨,我正在尝试获取一个引用了两个表的表的值。我的桌子看起来像这样 pages_content table page_id | content_id ------------------------ 1 | 4 6 | 10 pages table id | title ----------------- 1 | home 6 | contact content table id | content -
pages_content table
page_id | content_id
------------------------
1 | 4
6 | 10
pages table
id | title
-----------------
1 | home
6 | contact
content table
id | content
-----------------
4 | home page
10 | contact us
我需要引用pages\u content表并获取其他表中的值。
我试过这个
$content = DB::select('select * from pages_content pxc, content c where page_id = '.$page_id.' and content_id = c.id');
我有一个语法错误
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<p>home page</p> and content_id = c.id' at line 1 (SQL: select * from pages_content pxc, content c where page_id = <p>home page</p> and content_id = c.id) (View: /Applications/MAMP/htdocs/test/app/views/public/content.blade.php)
SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解在第1行的“主页和content_id=c.id”附近使用的正确语法(SQL:select*from pages_content pxc,content c其中page_id=主页和content_id=c.id)(视图:/Applications/MAMP/htdocs/test/app/views/public/content.blade.php)
我也尝试了一些方法。如果您需要我提供我尝试过的其他信息,或者如果您需要更多信息,请让我知道嗯,错误是不言自明的,您在查询中有语法错误,这是因为var$page_id等于主页(出于某种原因)
right syntax to use near '<p>home page</p> and content_id = c.id' at line 1
$contents = Page::find($page_id)->content;
foreach($contents as $c){
$c->pivot->created_at //or whatever you want to access
}