【问题标题】:Get SQL Column Value value from Nested Array从嵌套数组中获取 SQL 列值
【发布时间】:2015-01-25 18:44:42
【问题描述】:

为了更好地解释这一点,这里是我的代码:

        $mydate=Carbon::now()->addHours(8);
        $newdate=$mydate->toDateString();

        $myquery = DB::table('attendances')->where('date_only', '=', $newdate)->orderBy('logon','asc')->get();

        $counter=0;
        foreach ($myquery as $newquery) 
        {
        $query1[$counter]=$newquery->user_id;

        $finalquery[$counter]= DB::table('employees')->where('id', '=', $query1[$counter])->get();
        $counter++;

        }

        var_dump($finalquery[2]);

这段代码的输出是这样的:

      array(1) { [0]=> object(stdClass)#160 (6) { ["id"]=> int(23) ["firstname"]=> string(3) "Kim" ["lastname"]=> string(7) "Samsung" ["position"]=> string(10) "Programmer" ["created_at"]=> string(19) "2014-11-25 07:21:31" ["updated_at"]=> string(19) "2014-11-25 07:21:31" } } 

我想做的是访问它的一个值,所以我尝试了这个

         //I change
         var_dump($finalquery[2]);
         //to this
         var_dump($finalquery[2]->firstname);

但我得到的只是错误。错误代码 500

【问题讨论】:

  • 看起来$finalquery[2] 是一个数组...var_dump($finalquery[2][0]->firstname); 呈现什么?

标签: php sql database laravel eloquent


【解决方案1】:

您的var_dump 建议finalquery[2] 是一个对象数组,而不是对象本身。所以这行不通

var_dump($finalquery[2]->firstname);

这会

var_dump($finalquery[2][0]->firstname);

喜欢

foreach($finalquery[2] as $obj)
{
      echo $obj->firstname;
}

【讨论】:

    【解决方案2】:

    试试这个

       // not necessary
      foreach ($myquery as $key => $newquery) 
        {
            $query1[$key]=$newquery->user_id;
            $finalquery[$key]= DB::table('employees')->where('id', '=', $query1[$key])->get();
        }
    
        // necessary
    
        print_r($finalquery->toArray());  // if its array of objects I'm converting it into an array
            // or
        print_r($finalquery);
    

    编辑:哥们,我的建议是使用joins 这个单一查询将为您提供所有数据,无需使用foreach 和多个查询Reference

    $myquery = DB::table('attendances')->select('employees.id as emp_id','attendances.id as att_id',/*your fields*/)
            ->leftJoin('employees', 'employees.id','=','attendances.id')
            ->where('date_only', '=', $newdate)
            ->orderBy('attendances.logon','asc')->get();
    
    
        print_r($myquery->toArray());
    

    在控制器中

     return View::make('home', array('mydata' => $finalquery)); 
    

    在视图中:(对于刀片)

    @if($mydata && count($mydata) > 0)
        @foreach($mydata as $data)
    
            {{ $data->id }}<br>
            {{ $data->name }}<br>
            {{ $data->etc }}<br>
    
        @endforeach
    @else
        <p>sorry no data to display</p>
    @endif
    

    【讨论】:

    • 对不起,先生,我没有得到这个,但我要在视图中打印数组的每个值,知道怎么做吗?使用 foreach。无论如何感谢您的回答。
    • 等待生病更新...你要我们blade.php还是普通php??
    • 是的先生,我在我的控制器上使用此代码返回 View::make('home')->with('mydata',$finalquery);但我只是得到错误
    • 我只是不明白你的回答只是一个查询。我很抱歉这里的新手。当我使用你上次更新的代码时,我也得到了错误
    • 什么错误,在select() 中提及db 表字段名称,请参阅提及的参考
    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 2013-10-11
    • 2016-03-14
    • 2021-12-14
    • 1970-01-01
    • 2023-01-28
    • 2021-11-29
    • 2021-05-31
    相关资源
    最近更新 更多