douhao2856 2015-08-09 21:19
浏览 98

将两个雄辩的集合合并到一个对象属性中?

We can create empty objects in php using th stdClass and we can add properties to it like this:

$data = new stdClass;

$data->xyz = something;

But how could we add more things to a property? i mean something like this:

$data->xyz += somethingElse;

Is that possible?

What i want to do is this: i have to get some data from a database and i would like to store records of two tables in one property. To get data from a table and store it in a property i do this:

    $data = new stdClass;

    $data->gallery = \DB::table('RestaurantMenuOptions')
        ->select('Name', 'Image')
        ->get();

    $data->gallery2 = \DB::table('Promotions')
        ->select('Name', 'Image')
        ->get();

What i would like is to store them all in a single gallery property. By the way i use Laravel 5.

Edit 1

What i want is to join the data called from the table 'RestaurantMenuOptions' and 'Promotions' into one property of $data. I'm guessing if its possible to join two variables or if it's possible to get data from two unrelated tables using Eloquent?

What i've thought so far is to convert the models to array and then join them, but how do i convert the joined arrays to a model so that it in the views?

Edit 2

I found that it's possible to merge eloquent collections, but if the tables has the same type of PK and there are repeated records with same PK, they'll be overridden, so i decided to convert the collections to arrays merge them and access to them like them like this:

{{ $item['key'] }}

If someone has a better way to do it i'll apreciate it.

  • 写回答

1条回答 默认 最新

  • dongli8979 2015-08-09 21:22
    关注

    I think that a better way would be doing a for loop instead to make it somewhat more dynamic.

    Code:

    class Data {
        private $property, $property2;
    
        public function  __construct($property,$property2) {
            $this->Property = $property;
            $this->Property2 = $property2;
        }
    }
    
    $dataArray = array();
    
    $dataArray[] = new Data('Name',Image);
    $dataArray[] = new Data('Name',Image);
    
    print "<pre>";
    print_r($dataArray);
    print "</pre>";
    
    for ($i=0; $i < count($dataArray); $i++) { 
        // do something with the array of objects
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了