Laravel雄辩的独特价值观 [英] Laravel Eloquent distinct values

查看:66
本文介绍了Laravel雄辩的独特价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决了问题.

我正在尝试获取一个人的书,但是我的书桌上有不止一本书,因为一本书有不同的版本.当我列出一个人的所有书时,我不应该列出重复的书.

I'm trying to get an person's books, but there are more than one book in my book table since there are different editions of a book. When I list all books of a person, I shouldn't list duplicates.

这是我到目前为止所做的

Here's what I've done so far

人员模型

public function books() {
    return $this->belongsToMany('\App\Models\Thing', 'bookxauthor', 'person_id', 'thing_id');
}

PersonController.php

PersonController.php

$allbooks = Person::find($id)->books;  

这太好了,它列出了作者的所有书,但我不需要重复的书.

This is great, it lists all the books of an author, but I don't need duplicates.

下面的查询有效. type_id表示这是一本书.

The query below works. type_id means it's a book.

$findBooks = Person::with(array('books' => function($query)
            {
                $query->where('type_id',"=",3)->groupBy('original_name');
            }))->find($id);

            $allbooks = $findBooks->books;

推荐答案

您可以将groupBy函数用于集合 例如

You could use the groupBy function for collections Eg.

$allbooks = Person::find($id)->books->groupBy('name')->get();

这篇关于Laravel雄辩的独特价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆