【问题标题】:I want to print a column's value from a table using id generated from another table in Laravel我想使用从 Laravel 中的另一个表生成的 id 从表中打印列的值
【发布时间】:2017-11-20 06:48:05
【问题描述】:

我想打印前三个热门标签(使用最多的标签)的名称。 为了获得趋势 tags id,我使用了这个正在工作的查询

DB::table('review_tag')->select('tag_id')->groupBy('tag_id')->orderBy(DB::raw('count(tag_id)'), 'desc')->take(3)->get();

现在我需要使用 id 从表 tags 中获取标签的名称,我使用上述查询从 review_tag 表中获得

我试过用这个但是没用

B::table('review_tag')->join('tags', 'tags.id', '=', 'review_tag.tag_id')->select('tags.name')->groupBy('tag_id')->orderBy(DB::raw('count(tag_id)'), 'desc')->take(3)->get();

【问题讨论】:

    标签: php mysql laravel laravel-eloquent laravel-query-builder


    【解决方案1】:

    立即使用第一个查询的结果,例如:

    $results = ... first query
    
    B::table('tags')->select('name')->whereIn('id', $results->toArray())->get();
    

    【讨论】:

    • “PHP 错误:stdClass 类的对象无法转换为字符串”这是我得到的错误。你能帮忙吗?
    猜你喜欢
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多