集合的where子句总是返回空

时间:2017-02-20 15:02:57

标签: php laravel laravel-5

我有一个模型Sub HideUnhide_Discount() ' first reset all rows to be visible , later according to the value, unhide specific rows Range("MnthD_Row").EntireRow.Hidden = True Range("OOD_Row").EntireRow.Hidden = True Range("Leasing_Info").EntireRow.Hidden = True Select Case Range("Payment_Option") Case "Subscription" Range("MnthD_Row").EntireRow.Hidden = False Range("MnthD").Value = 0 Case "Lease" Range("OOD_Row").EntireRow.Hidden = False Range("Leasing_Info").EntireRow.Hidden = False Range("OOD").Value = 0 Case "Cash" Range("OOD_Row").EntireRow.Hidden = False Range("MnthD_Row").EntireRow.Hidden = False Range("OOD").Value = 0 End Select End Sub ,可以提取Imageshidden1等属性。

我试过了:

0

但它总是返回0。

但是,如果我使用SQLite对数据库进行原始查询:

$all_images = Image::all(); var_dump($all_images->first()->hidden); dd([ $all_images->where('hidden', "0")->count(), $all_images->where('hidden', 0)->count(), $all_images->where('hidden', 1)->count(), $all_images->where('hidden', "1")->count() ]); /* output sController.php:219:string '1' (length=1) array:4 [▼ 0 => 0 1 => 0 2 => 0 3 => 0 ]*/ {图像{1}} {隐藏{1}}

2 个答案:

答案 0 :(得分:0)

all将执行查询并返回一个集合。而是使用

dd([
    Image::where('hidden', "0")->count(),        
    Image::where('hidden', 1)->count(),        
]);

如果您必须使用集合,请执行以下操作:

dd([
   $allImages->filter(function ($value) { return $value->hidden == 0; })->count(),
   $allImages->filter(function ($value) { return $value->hidden == 1; })->count()
]);

不确定该集合是否与对象配合良好。

答案 1 :(得分:0)

您在集合而不是查询构建器上调用->where(...)

// This line will return a collection -  https://laravel.com/docs/5.4/eloquent-collections
$all_images = Image::all();

如果您不再需要可见和隐藏的图片

// Collection does not have a method where(..)

// To get images that are "hidden" do this:
$hidden = Image::where('hidden', 0)->get(); // Again the result will be a collection

// To get images that aren't hidden do this:
$visible = Image::where('hidden', 1)->get(); // Will result in a collection

如果您需要可见和隐藏的图片

// If you need both visible and hidden you could load them all at once:
$images = Image::get();

// Then separate them with collection()->filter() - https://laravel.com/docs/5.4/collections#method-filter

$hidden = $images->filter(function ($v, $k) {
    return $images[$k]->hidden;
});

$visible = $images->filter(function ($v, $k) {
    return !$images[$k]->hidden;
});
相关问题