簡體   English   中英

Laravel 5.2-簡單選擇查詢返回空數組

[英]Laravel 5.2 - Simple Select Query returns empty array

我正在運行一個簡單的查詢,但由於某種原因,它沒有給我正確的輸出。

應該通過復選框為用戶提供一個問題的多個答案。 有了這些答案,我試着從我的數據庫中獲取它們的ID。

這是我正在使用的代碼:

foreach($foodtrucktypes as $foodtrucktype)
{
    $type_id = DB::table('foodtruck_types')
        ->select('id')
        ->where('type', '= ', $foodtrucktype)
        ->get();
    echo $foodtrucktype."<br />";
    var_dump($type_id);
    echo "<br />";
    echo "<br />";
}

foreach($locations as $location)
{
    $location_id = DB::table('locations')
        ->select('id')
        ->where('name', '=', $location)
        ->get();

    echo $location."<br />";
    var_dump($location_id);
    echo "<br />";
    echo "<br />";
}

foreach($partytypes as $partytype)
{
    $party_id = DB::table('party_types')
        ->select('id')
        ->where('party_type' ,'=', $partytype)
        ->get();

    echo $partytype."<br :>";
    var_dump($party_id);
    echo "<br />";
    echo "<br />";
}

這是我得到的回應:

舒適食物陣列(0){}

Desserten數組(0){}

快餐數組(0){}

Henegouwen數組(1){[0] => object(stdClass)#195(1){[“id”] => int(2)}}

林堡數組(1){[0] => object(stdClass)#196(1){[“ id”] => int(3)}}

Bedrijfsfeest array(1){[0] => object(stdClass)#194(1){[“id”] => int(1)}}

Communiefeest array(1){[0] => object(stdClass)#197(1){[“id”] => int(2)}}

因此代碼可以工作,除了第一個foreach(foodtruck類型)外,它返回空數組,我不知道為什么。

這是DB的圖片

首先,我真的建議使用Eloquent模型......它使編寫代碼變得更好。

$type_id = DB::table('foodtruck_types')
    ->select('id')
    ->where('type', $foodtrucktype)
    ->get();

也許'= '的額外空間正在發揮作用。 你實際上不需要在你想要在Laravel上進行=比較->where()指定它。 如果這不起作用,嘗試var_dump() $foodtrucktype變量?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM