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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器