【问题标题】:For loop undefined offset error in Laravel query [closed]Laravel查询中的循环未定义偏移错误[关闭]
【发布时间】:2018-07-26 09:29:42
【问题描述】:

我正在尝试包含 for loop 以向我的查询添加 where 条件,它在 $ProductSubCategory 上返回 undefined offset 1 错误。 $ProductSubCategory 的内容是一个字符串数组,我想作为条件包含在内。

$Product = Product::select('id','ProductWorkpiece','ProductCategory','ProductName','ProductImage','ProductSubCategory')
    ->where('Status','=','1')
    ->Where(function ($query) use ($ProductSubCategory) {
        for ($i=1; $i <= $ProductSubCategory ; $i++) { 
            $Product->where('ProductSubCategory', '=', $ProductSubCategory[$i]);
        }
    })->get();

https://pastebin.com/uDSywsQv(Laravel 查询生成器 sn-p)

这是我想使用 Laravel 的查询构建器复制的 MySQL 查询,我该怎么做?

SELECT 
    `id`,
    `ProductWorkpiece`,
    `ProductCategory`,
    `ProductName`,
    `ProductImage`,
    `ProductSubCategory`
FROM
    `Product`
WHERE 
    `ProductSubCategory` = 'Laser Marking Machine' OR 
    `ProductSubCategory` = 'Dot Marking Machine' OR 
    `ProductSubCategory` = 'Digital Microscope' AND 
    `Status` = '1'

https://pastebin.com/AMWCz32g(所需的 MySQL sn-p)

【问题讨论】:

  • $i &lt;= $ProductSubCategory -- 改为$i &lt;= count($ProductSubCategory)
  • Eloquent 查询返回一个集合,所以如果这是一个集合,你也可以使用$i &lt;= $ProductSubCategory-&gt;count()
  • 这真是一个糟糕的解决方案。你为什么不使用whereIn 作为你的代码。类似于:$products = Product::whereStatus(1)-&gt;whereIn(''ProductSubCategory', $ProductSubCategory) -&gt;get 其中$ProductSubCategory 是一个数组。你可以在 Laravel 框架中保持你的代码非常干净。这样做是为了避免将来的麻烦

标签: sql laravel laravel-5 query-builder


【解决方案1】:

查看此$ProductSubCategory 不是数组或没有1 的索引。根据$ProductSubCategory 的内容,您可以使用几种方法。

解决方案 1:$ProductSubCategory 是一个字符串数组

$Product = Product::select('id','ProductWorkpiece','ProductCategory','ProductName','ProductImage','ProductSubCategory')
    ->where('Status','=','1')
    ->whereIn('ProductSubCategory', $ProductSubCategory)
    ->get();

这里你可以使用whereIn()并指定字符串数组,不需要循环任何东西。

解决方案 2.1:$ProductSubCategory 是记录的对象集合

$Product = Product::select('id','ProductWorkpiece','ProductCategory','ProductName','ProductImage','ProductSubCategory')
    ->where('Status','=','1')
    ->where(function ($query) use ($ProductSubCategory) {
        foreach($ProductSubCategory as $subcategory) {
            $query->orWhere('ProductSubCategory', '=', $subcategory->name);
        }
    })->get();

在这里,我们使用foreach 遍历每个对象记录,然后使用orWhere 而不是WHERE foo = bar AND foo = bar2 AND foo = bar3 从该对象引用name 字段

$subcategory->name 用作占位符,使用适用于您的字段名称。

注意在您的版本中,您使用了 function($query),但随后引用了 $Product 而不是 $query 来执行您的 where 子句。

编辑/更新

解决方案 2.2:$ProductSubCategory 是记录的对象集合

$subCategories = $ProductSubCategory->only('name')->toArray();
$Product = Product::select('id','ProductWorkpiece','ProductCategory','ProductName','ProductImage','ProductSubCategory')
        ->where('Status','=','1')
        ->whereIn('ProductSubCategory', $subCategories)
        ->get();

这与解决方案 1 相同,只是我们使用收集方法 only() 仅检索 name 字段(相应更改),然后转换名称 toArray() 以便它可以用作whereIn 有条件的。 这在我这边未经测试,但理论上应该可以工作

了解$ProductSubCategory 的内容和类型可能对我更有帮助。

【讨论】:

  • $ProductSubCategory 的内容和类型是数组([0] = 标记 [1] = Steel 等等)
  • 第一个解决方案应该适合你。
  • 是的,它的工作,谢谢你的帮助
【解决方案2】:

无法访问内部循环 $Product 尝试替换为 $query :

$query->where('ProductSubCategory', '=', $ProductSubCategory[$i]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多