laravel哪里有结果怪异

时间:2017-09-29 15:42:01

标签: php mysql laravel laravel-5 laravel-eloquent

产品型号

    public function country() {
        return $this->hasMany('App\Models\ProductCountry', 'product_id', 'id');
    }

控制器

$product = Product::where('mall_' . $this->mall_id, '=', 1)
    ->whereHas('country', function ($i) use ($me) {
        return $i->where('country_id', $me->country_id);
    })
    ->find($key);

原始SQL:

select * from `product` where `mall_3` = '1' and exists (select * from `product_country` where `product_country`.`product_id` = `product`.`id` and `country_id` = '109') and `product`.`id` = '3' and `product`.`deleted_at` is null limit 1

以上SQL返回无结果(当产品id = 3时

SQL下面的

返回正确的结果(当产品id = 6时)

select * from `product` where `mall_3` = '1' and exists (select * from `product_country` where `product_country`.`product_id` = `product`.`id` and `country_id` = '109') and `product`.`id` = '6' and `product`.`deleted_at` is null limit 1

不知道,查询看起来没什么问题

之前的项目我也遇到了这个问题,最后我再次使用find和循环而不是使用whereHas,但这次我再次遇到这个问题,并试图找出原因,但浪费了几个小时,仍然没有线索,下面是一种解决方法(忽略错误?)

$products = array();
foreach ($request->get('quantity') as $key => $var) {
    if ($var > 0) {
        $product = Product::with(['country'])->where('mall_' . $this->mall_id, '=', 1)
                    ->find($key);

        foreach ($product->country as $c) {
            if ($c->country_id == $me->country_id && !isset($products[$product->id])) {
                //Do something here...
            }
        }
    }
}

enter image description here

enter image description here

enter image description here

enter image description here

6 个答案:

答案 0 :(得分:4)

我创建了上面提到的表(给出完全相同的关系,表名,模型名称)并尝试了你的代码(通过给出硬编码值)

public function try1(){
    $me = 109;
    $key = 3;
    $product = Product::where('mall_3' , '=', 1)
        ->whereHas('country', function ($i) use ($me) {
            return $i->where('country_id', $me);
        })

        ->find($key);

    return $product;
}

public function try2(){
    $me = 109;
    $key = 6;
    $product = Product::where('mall_3' , '=', 1)
        ->whereHas('country', function ($i) use ($me) {
            return $i->where('country_id', $me);
        })

        ->find($key);

    return $product;
}

但获得了完美的结果

 {"id":3,"mall_1":1,"mall_2":1,"mall_3":1}

{"id":6,"mall_1":1,"mall_2":1,"mall_3":1}

请检查数据库是否一切正常,检查是否每个主键和外键都是"整数"。并检查"整理" db以及表格。

答案 1 :(得分:3)

这似乎与Laravel无关(因为直接使用MyAdmin运行查询时仍然没有结果)。

select * from `product`
where true
-- and `mall_3` = '1'
and exists (
    select * from `product_country`
    where true
    and `product_country`.`product_id` = `product`.`id`
    and `country_id` = '109'
)
and `product`.`id` = '3'
and `product`.`deleted_at` is null
limit 1

再次运行查询,逐个评论每个where条件(我添加了true第一个条件,以便轻松注释掉所有“and ...”行,看看你是否发现了问题开始了。

(我确实尝试创建两个表,顺便说一下,两个查询都会返回正确的结果。)

答案 2 :(得分:0)

首先要做的事情:

如果您正在使用eloquent(查找方法),则可以执行以下操作:

Product::find($id);

并返回产品模型,其中您提供的ID等于主键。话虽如此,没有必要使用whereHas()因为它没有效果。例如,您可以尝试使用find()get(),而不是first(),具体取决于您要查找的内容。

如果要打印出查询,可以使用->toSql();结束查询,它会将查询打印为字符串(帮助很多!)。

我没有太注意目标,但我确定你是否使用

dd(Product::where('mall_' . $this->mall_id, '=', 1)
->whereHas('country', function ($i) use ($me) {
    return $i->where('country_id', $me->country_id);
})
->toSql());

您将能够比较您正在做的事情(因为您知道正确的结果或查询应该是什么样的)并且能够弄明白。

毕竟,我们确实需要运动来探索我们的想法!

答案 3 :(得分:0)

试着希望它会对你有所帮助,

 $products = array();
    foreach ($request->get('quantity') as $key => $var) {
        if ($var > 0) {
            $product = Product::where('mall_' . $this->mall_id, '=', 1)
                        ->leftjoin('product_country','product_country.product_id','=','product.id')->get();
            $products[$product->id] = ['product_id'=>$product->id,'country_id'=>$product->country_id];              
        }
    }

答案 4 :(得分:0)

我认为您应该将产品模型中的关系概念从hasMany更改为belongsToMany 同时通过将国家模型定义为第一个参数,将中间表名称定义为第二个参数,然后将外键和主键定义为第3个和第4个参数来更新参数。 belongsToMany('App \ Models \ Country','product_country','product_id','country_id')

产品型号

public function country() {
    return $this->belongsToMany('App\Models\Country', 'product_country','product_id', 'country_id');
}

因为每个国家/地区可能属于许多产品,并且每个产品可能属于许多国家/地区。

答案 5 :(得分:0)

我知道这个帖子有点陈旧,但我的团队实际上与Laravel和MySQL 5.7.18上的whereHas(MySQL EXISTS)有一个非常相似的问题。

一旦我们升级了MySQL,同样的查询开始完全按预期返回。我还没弄明白这是不是5.7.18的错误,但我怀疑它可能是。