Php 提取数据透视表中的所有记录

Php 提取数据透视表中的所有记录,php,laravel,Php,Laravel,在我的数据库中,通知和状态表之间存在多对多关系。因此,数据透视表存储了通知id、状态id和值。现在,在我的模型中,我编写了一个函数historysStatus,它从pivot表中提取该值并创建_at public function historystatus() { $o_status = Status::where('name','health')->first(); $o_response = $this->statuses()->where('status

在我的数据库中,通知和状态表之间存在多对多关系。因此,数据透视表存储了通知id、状态id和值。现在,在我的模型中,我编写了一个函数historysStatus,它从pivot表中提取该值并创建_at

public function historystatus()
{
    $o_status = Status::where('name','health')->first();
    $o_response = $this->statuses()->where('status_id', $o_status->id)
    ->select('values','created_at')->first();

        if($o_response === null){
            return false;
        }


     return ['value' => $o_response->values,
             'timestamp' => $o_response->created_at];
}
我希望遍历与特定通知相关联的所有值和时间戳。因此,在blade中,我编写了for each循环,以遍历所有值并在处创建。

@foreach($notes as$notification)
{{$notificationHealth['timestamp']}
{{$notificationHealth['value']}。
@endforeach

相反,它只给了我4次最后的记录。sql查询是否有问题?如果您有任何建议和想法,我将不胜感激。

foreach
中尝试使用
$notification->historystatus('health')而不是
$notes->historystatus('health')

@foreach($notes as$notification)
{{$notificationHealth['timestamp']}
{{$notificationHealth['value']}。
@endforeach

foreach
中尝试使用
$notification->historystatus('health')而不是
$notes->historystatus('health')

@foreach($notes as$notification)
{{$notificationHealth['timestamp']}
{{$notificationHealth['value']}。
@endforeach
这是我想出的简单答案。以及叶片侧面的foreach环


这是我想出的简单答案。以及叶片侧面的foreach环

使用$notificationHealth=$notification->historystatus('health');而不是$notificationHealth=$notes->historystatus('health');输入错误即使有了更改,它也不起作用。为什么要用CakePHP标记它?代码看起来像Laravel…使用$notificationHealth=$notification->historystatus('health');而不是$notificationHealth=$notes->historystatus('health');输入错误即使有了更改,它也不起作用。为什么要用CakePHP标记它?代码看起来像拉威尔。。。
@foreach($notes as $notification)
    <?php $notificationHealth = $notes->historystatus('health'); ?>
    <tr>   
        <td>{{ $notificationHealth['timestamp'] }}</td>                         
        <td> {{ $notificationHealth['value'] }}</td>.
    </tr>    
@endforeach
</tbody>
    @foreach($notes as $notification)
    <?php $notificationHealth = $notification->historystatus('health'); ?>
    <tr>   
        <td>{{ $notificationHealth['timestamp'] }}</td>                         
        <td> {{ $notificationHealth['value'] }}</td>.
    </tr>    
@endforeach
public function history($s_type)
    {
        if (!in_array($s_type, ['speed', 'health'])){
            return false;
        }

        $o_status = Status::where('name', strval($s_type))->first();
        $o_response = $this->statuses()->where('status_id', $o_status->id)
        ->select('values', 'created_at')->orderBy('created_at','DESC')->get();

        if($o_response === null || $o_response->count() === 0){
            return false;
        }

        $a_ret = [];

        foreach ($o_response as $o_status) {
            $a_ret[] = [
                'value' => $o_status->values,
                'timestamp' => $o_status->created_at
            ];
        }

        return $a_ret;

    }