duanfu4446 2013-12-05 12:02
浏览 30
已采纳

Laravel 4 DB :: table - Foreach

I'm trying to run a sql query and return all values into an array of the tables names...

This is what I have so far:

$vehiclemodeldata = DB::table('model')->where('model_id', $viewData['model_id'])->get();
$row = $vehiclemodeldata->row();
foreach ($row as $key => $value){

   $viewData['vehiclemodeldata_'.$key]= $value;

}

What are your throughts?

EDIT

I've tried this it seems to bring it back in an array but I can't access the array for some reason.

$VehicleModel = DB::table('model')->where('model_id', $viewData['model_id'])->get();

Thanks

  • 写回答

2条回答 默认 最新

  • dougaoshang0237 2013-12-05 13:28
    关注

    This is because $vehiclemodeldata = DB::table('model')->where('model_id', $viewData['model_id'])->get(); actually returns an object instead of an array.

    Your foreach loop is correct. You can later access properties in the array by using syntax like this $viewData['vehiclemodeldata_1']->yourProperty; which is used to access properties of objects.

    Hope it answers the question.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据