Php 访问;创建于;透视表项的时间戳

Php 访问;创建于;透视表项的时间戳,php,laravel,eloquent,Php,Laravel,Eloquent,我需要在我读取的透视表条目中的时间戳处创建的pivot\u的值 访问数据透视表数据已经完成,我使用Eloquent进行访问: public function states() { return $this->belongsToMany('App\Classes\DispatchState')->withTimestamps(); } public function getStatesForDispatch($dispatchReference) {

我需要在我读取的透视表条目中的时间戳处创建的pivot\u的值

访问数据透视表数据已经完成,我使用Eloquent进行访问:

    public function states() {
        return $this->belongsToMany('App\Classes\DispatchState')->withTimestamps();
    }

public function getStatesForDispatch($dispatchReference) {
    $dispatch = new Dispatch();
    $dispatch = $dispatch->getDispatch($dispatchReference);
    $statusArray = [];
    $states = $dispatch->states()->get();

    foreach ($states as $status) {
        $statusArray[] = $status;
    }

    return $statusArray;
}
但是如何访问在条目中创建的
透视以读取时间戳?我现在有点不知所措。

来自。访问数据透视的方法是:

$user = User::find(1);

foreach ($user->roles as $role)
{
    echo $role->pivot->created_at;
}
因此,在您的情况下,它将是:

public function getStatesForDispatch($dispatchReference) {
    $dispatch = new Dispatch();
    $dispatch = $dispatch->getDispatch($dispatchReference);

    // ->all gets the internal array from the Laravel Collection
    $statusArray = $dispatch->states->all();


    foreach ($statusArray as $status) 
    {
        $createdAt = $status->pivot->created_at;
        // do something with the created_at from the pivot table
    }

    return $statusArray;
}
从。访问数据透视的方法是:

$user = User::find(1);

foreach ($user->roles as $role)
{
    echo $role->pivot->created_at;
}
因此,在您的情况下,它将是:

public function getStatesForDispatch($dispatchReference) {
    $dispatch = new Dispatch();
    $dispatch = $dispatch->getDispatch($dispatchReference);

    // ->all gets the internal array from the Laravel Collection
    $statusArray = $dispatch->states->all();


    foreach ($statusArray as $status) 
    {
        $createdAt = $status->pivot->created_at;
        // do something with the created_at from the pivot table
    }

    return $statusArray;
}

这些问题。。。为什么不阅读文档?如果没有,请转到并搜索pivot。这些问题。。。为什么不阅读文档?如果没有,请转到并搜索pivot。