Laravel 或 PHP foreach issue on DB::Table vs. Model::get()
Laravel or PHP foreach issue on DB::Table vs. Model::get()
下面的代码工作正常:
$data = Structures::select("id", "parent_id", "category", "type", "subType", "variant", "description")
->orderBy('category')
->orderBy('type')
->orderBy('subType')
->orderBy('variant')
->get() ;
return $this->fillCategories($data) ;
fillCategories()
函数用父子关系填充 JSON 树。
但是,下面的代码不起作用。
$data = DB::table("categories AS t1")
->select("t1.id", "t1.name AS category", "t2.id as typeId", "t2.name as type", "t3.id as subTypeId", "t3.name as subType", "t4.id as variantId", "t4.name as variant")
->leftJoin("categories AS t2", "t2.parent_id", "=", "t1.id")
->leftJoin("categories AS t3", "t3.parent_id", "=", "t2.id")
->leftJoin("categories AS t4", "t4.parent_id", "=", "t3.id")
->whereNull("t1.parent_id")
->orderBy("t1.name")
->orderBy("t2.name")
->orderBy("t3.name")
->orderBy("t4.name")
->get() ;
foreach ($data as $v) {
Log::info("cat: " . json_encode($v));
}
return $this->fillCategories($data) ;
本地日志returns值,但是当$data
传递给fillCategories()
时函数中的foreach忽略了$data
!
fillCategories()
中 foreach
的片段
...
$select = array() ;
$child = array() ;
$cat = null ;
$type = null ;
$sub = null ;
$first = true ;
$clear = true ;
$label = null ;
$value = null ;
$prev = null ;
Log::info("fillCat: data = " . json_encode($data)) ;
foreach ($data as $v):
Log::info("fillCat forloop: v($v->cat, $v->type, $v->subType, $v->variant)") ;
$parent = null ;
$isKid = false ;
$isGrandKid = false ;
...
我现在假设将 $data
从 DB::table
而不是 Facades\App\Model\Structures::get()
传递给函数会在翻译中不知何故丢失。
谢谢 Faiz,显然 orderBy
是问题所在! (!) 如果我把它拿出来,fillCategories()
foreach
处理数据。
$data = DB::table("categories AS t1")
->select("t1.id", ...)
->leftJoin("categories AS t2", "t2.parent_id", "=", "t1.id")
->leftJoin("categories AS t3", "t3.parent_id", "=", "t2.id")
->leftJoin("categories AS t4", "t4.parent_id", "=", "t3.id")
->whereNull("t1.parent_id")
->orderBy("t1.name")
->orderBy("t2.name")
->orderBy("t3.name")
->get() ;
这行得通。也许 Taylor Otwell 可以告诉我原因!
下面的代码工作正常:
$data = Structures::select("id", "parent_id", "category", "type", "subType", "variant", "description")
->orderBy('category')
->orderBy('type')
->orderBy('subType')
->orderBy('variant')
->get() ;
return $this->fillCategories($data) ;
fillCategories()
函数用父子关系填充 JSON 树。
但是,下面的代码不起作用。
$data = DB::table("categories AS t1")
->select("t1.id", "t1.name AS category", "t2.id as typeId", "t2.name as type", "t3.id as subTypeId", "t3.name as subType", "t4.id as variantId", "t4.name as variant")
->leftJoin("categories AS t2", "t2.parent_id", "=", "t1.id")
->leftJoin("categories AS t3", "t3.parent_id", "=", "t2.id")
->leftJoin("categories AS t4", "t4.parent_id", "=", "t3.id")
->whereNull("t1.parent_id")
->orderBy("t1.name")
->orderBy("t2.name")
->orderBy("t3.name")
->orderBy("t4.name")
->get() ;
foreach ($data as $v) {
Log::info("cat: " . json_encode($v));
}
return $this->fillCategories($data) ;
本地日志returns值,但是当$data
传递给fillCategories()
时函数中的foreach忽略了$data
!
fillCategories()
foreach
的片段
...
$select = array() ;
$child = array() ;
$cat = null ;
$type = null ;
$sub = null ;
$first = true ;
$clear = true ;
$label = null ;
$value = null ;
$prev = null ;
Log::info("fillCat: data = " . json_encode($data)) ;
foreach ($data as $v):
Log::info("fillCat forloop: v($v->cat, $v->type, $v->subType, $v->variant)") ;
$parent = null ;
$isKid = false ;
$isGrandKid = false ;
...
我现在假设将 $data
从 DB::table
而不是 Facades\App\Model\Structures::get()
传递给函数会在翻译中不知何故丢失。
谢谢 Faiz,显然 orderBy
是问题所在! (!) 如果我把它拿出来,fillCategories()
foreach
处理数据。
$data = DB::table("categories AS t1")
->select("t1.id", ...)
->leftJoin("categories AS t2", "t2.parent_id", "=", "t1.id")
->leftJoin("categories AS t3", "t3.parent_id", "=", "t2.id")
->leftJoin("categories AS t4", "t4.parent_id", "=", "t3.id")
->whereNull("t1.parent_id")
->orderBy("t1.name")
->orderBy("t2.name")
->orderBy("t3.name")
->get() ;
这行得通。也许 Taylor Otwell 可以告诉我原因!