douzhi2012 2013-05-09 03:16
浏览 53
已采纳

获取数据透视表和相关表列值Laravel 3

Good evening peeps, I'm playing a little bit with Laravel, and I have this situation. I have 2 models and 3 tables to display vital signs from a pacient.

Table pacient:
id
names
birthdate

Table vital_sign:
id
description
unit

table pacient_vital_sign
id
pacient_id
vital_sign_id
first_value
second_value
date_time_taken

And the 2 models:

class Pacient extends Eloquent{
        public static $timestamps = false;

        public function vital_signs(){
          return $this->has_many_and_belongs_to('VitalSign', 'pacient_vital_sign', 'pacient_id', 'vital_sign_id');
        }
    }

class VitalSign extends Eloquent{
        public static $timestamps = false;
        public static $table = 'vital_signs';
        public function pacients(){
          return $this->has_many_and_belongs_to('Pacient', 'pacient_vital_sign', 'vital_sign_id', 'pacient_id');
        }
    }

And for my view I'm sending the pacient whose vital signs I want to display:

$pacient = Pacient::where('active', '=', 1)->first();

So, based on the documentation I could do this to show the pivotal table data:

@foreach($pacient->vital_signs as $sv)
    <tr>
        <td>{{ $sv->pivot->date_time_taken }}</td>
        <td>{{ $sv->description }}</td>
        <td>{{ $sv->pivot->first_value }} &nbsp; {{ $sv->pivot->second_value }}</td>
    </tr>
    @endforeach

But this way, neither the date_time_taken value nor first_value or second_value is showed. Description is displayed.

Now, if I do something like this

@foreach($pacient->vital_signs()->pivot()->get() as $sv)
    <tr>
        <td>{{ $sv->date_time_taken }}</td>
        <td>{{ $sv->description }}</td>
        <td>{{ $sv->first_value }} &nbsp; {{ $sv->second_value }}</td>
    </tr>
    @endforeach

date_time_taken, first_value and secon_value are displayed but I dont know how to reverse the relationship in order to display the value for the related table column, in this case, description from vital_sign table

So I both cases, I'm missing something. Any ideas? What I'm missings?

Thanks

  • 写回答

2条回答 默认 最新

  • duanpie2834 2013-05-15 15:28
    关注

    On the Docs:

    By default only certain fields from the pivot table will be returned (the two id fields, and the timestamps). If your pivot table contains additional columns, you can fetch them too by using the with() method

    So in my case, on both models:

    return $this->has_many_and_belongs_to('VitalSign', 'pacient_vital_sign', 'pacient_id', 'vital_sign_id')->with('col1', 'col2', 'coln');
    

    and this will work:

    @foreach($pacient->vital_signs as $sv)
        <tr>
            <td>{{ $sv->pivot->date_time_taken }}</td>
            <td>{{ $sv->description }}</td>
            <td>{{ $sv->pivot->first_value }} &nbsp; {{ $sv->pivot->second_value }}</td>
        </tr>
    @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。