Php Laravel 5检索wherepivot中的第一条记录

Php Laravel 5检索wherepivot中的第一条记录,php,laravel,eloquent,Php,Laravel,Eloquent,我有三张桌子: *document* [id, core_document] *person* [id, name] *documentPersonsRole* [document_id, person_id, role] (where role could be accused or victim) 是否可以创建一个快速方法来从属于多个关系返回第一条记录?以下是我的代码,来自模型文件: public function documentPersonsRole() { retu

我有三张桌子:

*document* [id, core_document]  
*person* [id, name]  
*documentPersonsRole* [document_id, person_id, role] (where role could be accused or victim) 
是否可以创建一个快速方法来从属于多个关系返回第一条记录?以下是我的代码,来自模型文件:

public function documentPersonsRole()
{
    return $this->belongsToMany('App\Models\Person', 'document_person_role')
        ->withPivot('role')
        ->withTimestamps();
}

public function accused()
{
    return $this->belongsToMany('App\Models\Person', 'document_person_role')
        ->withPivot('role')
        ->wherePivot('role', 'accused')
        ->withTimestamps();
}

public function victim()
{
    return $this->belongsToMany('App\Models\Person', 'document_person_role')
            ->withPivot('role')
            ->wherePivot('role', 'victim')
            ->withTimestamps();    
}
当我称之为:

App\Models\Document::with('accused')->first()
=> App\Models\Document {#3118
     id: 1,
     core_document: "Iure aut eum aut ex et. Magni aliquam illo voluptatem non repellat. Maxime occaecati reiciendis veniam quod neque reiciendis dolores. Eaque quis molestiae dolorem. Et rerum veniam animi sit beatae inventore voluptas. Aut ea atque nulla quis quam incidunt iusto voluptas. Aut corrupti voluptas minima unde dicta vero aut veritatis. Voluptas vitae nam mollitia quasi porro id quod ut.",         
     accused: Illuminate\Database\Eloquent\Collection {#3127
       all: [
         App\Models\Person {#3139
           id: 41,
           name: "Jamie",               
           pivot: Illuminate\Database\Eloquent\Relations\Pivot {#3138
             document_id: 1,
             person_id: 41,
             role: "accused",
             created_at: "2018-10-24 03:55:23",
             updated_at: "2018-10-24 03:55:23",
           },
         },
       ],
     },
   }
您可以看到,是一个只有一条记录的集合,当我在客户端收到它时,我必须从数组中提取这条记录,但我想处理对象

当我尝试这样的事情时:

public function accused()
    {
        return $this->belongsToMany('App\Models\Person', 'document_person_role')
            ->withPivot('role')
            ->wherePivot('role', 'accused')
            ->withTimestamps()->first();
    }
这就是我得到的错误:

BadMethodCallException,消息为“调用未定义的方法/Database/Query/Builder::AddagerConstraints()”


如何使用first()来检索和对象,而不是使用一条记录的数组。

如何访问它

您可能必须直接调用该方法,而不是让Laravel尝试通过神奇的方法动态获取该方法:

$contained=$document->contained()

或者,定义一个:

这将允许您使用
$contained=$document->contained

通常,当您定义一个返回关系的方法
bar()
时,当您调用
$foo->bar
时,Laravel将在PHP中钩住
\uu get()
魔术方法,在幕后某处调用
bar()->get()
。因为您已经在关系上使用
first()
运行查询,所以Laravel希望对一个关系运行查询,但最终得到了您的模型

编辑:

如果您仍然希望能够快速加载关系,这里有另一种方法:

在文件中:

public function accusedPeople()
{
    return $this->belongsToMany('App\Models\Person', 'document_person_role')
        ->withPivot('role')
        ->wherePivot('role', 'accused')
        ->withTimestamps();
}

public function getFirstAccusedAttribute()
{
    return $this->accusedPeople
        ->first();
}
在控制器中:

// If you're eager loading, the relationship to eager load is `accusedPeople`
$document = Document::with('accusedPeople')->first();

// The accused person is accessed with the `first_accused` property
$accused = $document->first_accused;

// if you're returning the model directly for json:
// $document->append('first_accused');
试试这个:

App\Models\Document::with(['accused' => function($query){
    $query->first();
}])->first()

这将查询
belongstone
以仅返回第一行。

不完全确定,但您是否尝试过
belongstone
而不是
belongstony
?@i--我非常确定
belongstone()
不存在
->belongsTo()
?仍然返回数组$vm0。文档[0]。被告[{…},ob ob ob Observer]0:{…}length:1ob:Observer{value:Array(1),dep:dep,vmCount:0}proto:Array我是这样做的,但结果证明它做错了。getAccountDattribute()-此函数允许使用此函数并检索一个关系:$document->contained谢谢!但是我如何在控制器中使用它而不是这个公共函数索引(Request$Request){$query=Document::with('contained');$documents=$query->get();return response()->json($documents,200);}在客户端接收数据后使用:$document->accused@MaximStogniy对不起,我不知道你的评论是什么意思?我修正了我的评论,你能帮我吗?@MaximsTogney你不能以当前形式加载它(不能将
与('contained')
一起使用)。我如何收集对象文档,包括[id,core_文档,被告(对象)]?或者唯一的选项是一个带有单个元素的数组,该元素需要在客户端上转换为对象?
App\Models\Document::with(['accused' => function($query){
    $query->first();
}])->first()