将多对多原始 sql 语句转换为 eloquent 查询构建器
Convert a many to many raw sql statement to eloquent query builder
我需要翻译这个有效的 sql 语句:
select model_names.name
FROM blog_posts
INNER JOIN model_names_relations
INNER JOIN model_names
ON blog_posts.id = model_names_relations.blog_post_id and model_names.id = model_names_relations.model_name_id
WHERE blog_posts.id = '12'
到 laravel 查询生成器。我没有使用完整的 orm,所以我不能使用 belongstomany 特性。我仅限于查询生成器。
我试过这个:
$query = ( new DbSql )->db()->table( 'blog_posts' )
->join( 'model_names_relations', 'blog_post_id.id', '=', 'model_names_relations.blog_post_id' )
->join( 'model_names', 'model_names.id', '=', 'model_names_relations.model_name_id' )
->where( 'blog_posts.id', '12')
->select( 'model_names.name' )
->get();
var_dump( $query );
exit;
但它不起作用我得到:
protected 'message' => string 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'blog_post_id.id' in 'on clause' (SQL: select model_names
.name
from blog_posts
inner join model_names_relations
on blog_post_id
.id
= model_names_relations
.blog_post_id
inner join model_names
on model_names
.id
= model_names_relations
.model_name_id
where blog_posts
.id
= 12)' (length=357)
private 'string' (Exception) => string '' (length=0)
正确的转换语法是什么?
这里是 Laravel 查询生成器
$query =DB::table('blog_posts')
->join('model_names_relations', 'blog_posts.id', '=', 'model_names_relations.blog_post_id')
->join('model_names', 'model_names.id', '=', 'model_names_relations.model_name_id')
->where('blog_posts.id', '12')
->get();
但是你的错误意味着 blog_post_id
table 中没有 'id'。
我需要翻译这个有效的 sql 语句:
select model_names.name
FROM blog_posts
INNER JOIN model_names_relations
INNER JOIN model_names
ON blog_posts.id = model_names_relations.blog_post_id and model_names.id = model_names_relations.model_name_id
WHERE blog_posts.id = '12'
到 laravel 查询生成器。我没有使用完整的 orm,所以我不能使用 belongstomany 特性。我仅限于查询生成器。
我试过这个:
$query = ( new DbSql )->db()->table( 'blog_posts' )
->join( 'model_names_relations', 'blog_post_id.id', '=', 'model_names_relations.blog_post_id' )
->join( 'model_names', 'model_names.id', '=', 'model_names_relations.model_name_id' )
->where( 'blog_posts.id', '12')
->select( 'model_names.name' )
->get();
var_dump( $query );
exit;
但它不起作用我得到:
protected 'message' => string 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'blog_post_id.id' in 'on clause' (SQL: select
model_names
.name
fromblog_posts
inner joinmodel_names_relations
onblog_post_id
.id
=model_names_relations
.blog_post_id
inner joinmodel_names
onmodel_names
.id
=model_names_relations
.model_name_id
whereblog_posts
.id
= 12)' (length=357) private 'string' (Exception) => string '' (length=0)
正确的转换语法是什么?
这里是 Laravel 查询生成器
$query =DB::table('blog_posts')
->join('model_names_relations', 'blog_posts.id', '=', 'model_names_relations.blog_post_id')
->join('model_names', 'model_names.id', '=', 'model_names_relations.model_name_id')
->where('blog_posts.id', '12')
->get();
但是你的错误意味着 blog_post_id
table 中没有 'id'。