【发布时间】:2025-12-02 06:20:03
【问题描述】:
我正在尝试使用 Eloquent ORM 编写此 SQL 查询,但仍然没有成功:
SELECT *
FROM article
LEFT JOIN article_category
ON article.category_id = article_category.id
WHERE article_category.name_url = 'html'
LIMIT 10`
这是我目前想出的(我尝试只用一个查询来编写它,就像上面一样):
ArticleCategory::where('name_url', '=', 'html')->with('articles')->get();
但它显示错误:
Column not found:
1054 Unknown column 'article.article_category_id' in 'where clause'
(SQL: select * from `article` where `article`.`article_category_id` in (1))
我的模型:
class Article extends Eloquent {
protected $table = 'article';
public function categories() {
return $this->belongsTo('ArticleCategory', 'category_id');
}
}
class ArticleCategory extends Eloquent {
protected $table = 'article_category';
public function articles() {
return $this->hasMany('Article');
}
}
【问题讨论】:
标签: php mysql sql laravel eloquent