【问题标题】:Laravel 5: DB::table() and relationship in EloquentLaravel 5:DB::table() 和 Eloquent 中的关系
【发布时间】:2018-05-03 23:23:16
【问题描述】:

我在 Community 和 LangCommunity 之间有一个 hasMany 关系,我正在尝试按名称字段排序而不丢失视图中的关系。

模特社区

protected $fillable = ['province_id', 'active', 'address', 'phone', 'google_map'];

public function langs()
{
    return $this->hasMany('App\Models\LangCommunity');
}

LangCommunity 模型

protected $fillable = ['community_id', 'lang_id', 'name', 'text'];

public function community()
{
    return $this->belongsTo('App\Models\Community');
}

控制器中,我有:

$data['communities'] = DB::table('communities')
        ->join('lang_communities', 'communities.id', '=', 'lang_communities.community_id')
        ->select('communities.*', 'lang_communities.name')
        ->where('lang_communities.lang_id', '1')
        ->orderBy('lang_communities.name', 'asc')
        ->paginate($num);

这项工作,但我不能使用视图中的关系。我试图通过以下方式做到这一点:

$data['communities'] = Community::with(['langs' => function($query)
    {
        $query
            ->where('lang_id', '1')
            ->join('communities', 'communities.id', '=', 'lang_communities.community_id')
            ->orderBy('lang_communities.name', 'asc');
    }])->paginate($num);

但这不是按名称排序的。有什么想法吗?

【问题讨论】:

    标签: php laravel sorting inner-join relationship


    【解决方案1】:

    你试过了吗:

    $data['communities'] = Community::with(['langs' => function ($q) {
            $q->orderBy('name', 'asc');
        }])
        ->whereHas('langs', function ($query) {
            $query->where('lang_id', 1);
        })->get();
    

    【讨论】:

    • 是的,但这给了我错误:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'lang_communities.lang_id' in 'where Clause' (SQL: select count(*) as aggregate from communities 其中lang_communities.lang_id = 1)
    • 好吧,我没有错误,但这并没有按名称简称。这是两个查询的结果:select * from communities` where exists (select * from lang_communities where communities.id = lang_communities.community_id and lang_id = '1')` select * from lang_communities where lang_communities.community_id in ('1', '2', '3', '4', '5', '6', '7', '8', '9 ','10','11','12','13','15','16','17','18','19','20','21','22', '23'、'24'、'25'、'26'、'27'、'28'、'29'、'30'、'31'、'32'、'33'、'34'、'35 ', '36', '37', '38', '39', '40') 按nameasc 排序`
    • 嗯好的 :( 在您的第一个解决方案中,您不能使用关系,但您可以访问表 lang_communities 的字段,例如:$data['communities']->name。也许是最简单的方法。
    • 是的,我可以。问题是我必须在几个视图和部分中实现它,并且我必须重构所有代码;但没关系,如果不可能,我得花几个小时;)还是谢谢。
    【解决方案2】:

    好的。我已经设法解决了;)

    $data['communities'] = Community::join('lang_communities', 'communities.id', '=', 'lang_communities.community_id')
                ->select('communities.*', 'lang_communities.name')
                ->where('lang_communities.lang_id', '1')
                ->orderBy('lang_communities.name', 'asc')
                ->paginate($num);
    

    【讨论】:

      猜你喜欢
      • 2021-05-09
      • 2015-04-16
      • 2021-01-14
      • 2016-11-26
      • 2016-08-16
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      • 2017-02-22
      相关资源
      最近更新 更多