【问题标题】:join and where clause on child in Laravel 5 EloquentLaravel 5 Eloquent 中关于孩子的 join 和 where 子句
【发布时间】:2015-04-14 09:02:07
【问题描述】:

我正在使用 Laravel 5 开发应用程序,我有 2 个表 productCategoryproducttable 有一个列 category_id 引用 idcatgeory

我还有两个型号CategoryProduct

问题: 我只需要向我的Category 模型添加一个简单的函数,它返回由product.sx_code = 0 与雄辩的产品加入的类别

【问题讨论】:

    标签: eloquent laravel-5 jointable


    【解决方案1】:

    您可以只添加一个普通的关系方法并附加一个where 条件:

    public function specialProducts(){
        return $this->hasMany('App\Product')->where('sx_code', 0);
    }
    

    【讨论】:

    • @JadJoubran 那么你不能真正把它放在关系声明中。您必须在查询时应用它:$category->specialProducts()->where('sx_code', $code)->get()
    • 好的,但就我而言,我需要指定列名。我正在尝试->where('table.sx_code', $code)->get();
    • 在 cmets 中并不是帮助您解决此问题的最佳位置。请提出一个新问题,提供所有必要的详细信息。
    猜你喜欢
    • 2015-08-06
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    相关资源
    最近更新 更多