【问题标题】:Simple MySQL Select Query Issue in LaravelLaravel 中的简单 MySQL 选择查询问题
【发布时间】:2015-01-11 11:23:20
【问题描述】:

这是我的 Laravel 下拉查询

我在哪里得到$CountryID 并从显示在选择框中的区域表中选择区域名称

但我不知道如何在这里使用where 条件

我做错了什么?

$CountryID = Input::get('CountryID');
$roles = DB::table('region')->lists('regionname');
foreach ($roles as $value) 
{
echo "<option value=".$value.">".$value."</option>";
}
echo "</select>";
}

查询:简单来说

如何更改此查询

$roles= DB::select( DB::raw("SELECT regionname FROM region WHERE CountryID = '$CountryID'") );

到本机查询:

$roles = DB::table('region')->lists('regionname');

注意: 现在我正在获取区域表中的所有字段

【问题讨论】:

    标签: php mysql laravel laravel-4


    【解决方案1】:

    您所要做的就是添加一个 where 子句。

    $CountryID = Input::get('CountryID');
    $roles = DB::table('region')->where('countryId', $CountryID)->lists('regionname');
    foreach ($roles as $value) 
    {
    echo "<option value=".$value.">".$value."</option>";
    }
    echo "</select>";
    }
    

    【讨论】:

    • 它说“数组到字符串的转换”
    • 你的查询相当于 select regionname from region where countryid = $countryid right ?
    • 如果我需要 ->lists('regionname','regionid');如何在 foreach 中打印“regionid”
    • “数组到字符串的转换”在哪一行?看起来您的 $CountryID 是一个数组,或者您的 $value 是一个数组。
    【解决方案2】:

    我会将您的查询更改为:

    $results = DB::table('region')->select('regionname', 'regionid')
    ->where('countryId', $CountryID)
    ->get();
    

    这将返回一个 stdClass 对象数组,您可以从中访问属性:

    foreach($results as $result)
    {
        echo $result->regionid;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      相关资源
      最近更新 更多