我有一个包含列的People表:person、name、text。列person只能具有以下值:physician、dentist、nurse等...
我想使用Laravel Resource通过api发送它,所以我创建了基本的PeopleResource.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class PeopleResource extends Resource
{
/**
* Transform the resource in
我希望withCount包含不存在的关系的计数。 例如,假设我有一个名为people的表和一个名为pets的表。 people有多个pets,但pets可以有一个空people_id。我想要获得每个people的pets的计数,但我还想包括没有people_id的pets。 我知道我可以简单地执行People::withCount('pets')->get(),但这不包括people_id为空的pets的计数。 我正在寻找的结果将类似于: person_name | pets_count
John | 2
Mike | 3
Joan
我有Model Category和products,Model Category有2个关系:
public function children()
{
return $this->hasMany(self::class, 'parent_id');
}
public function parent()
{
return $this->belongsTo(self::class, 'parent_id')->withoutGlobalScope('active');
}
和模型产品有:
public functi
我从需要分组的数据库中获取数据,所以我将数据库结果集转换为数组,然后将其传递给laravel收集助手,但我给出了错误。
Call to undefined function collect()
代码
$user_profile=collect(UserProfileItem::where('type', "age_group")->get()->toArray())->groupBy("age_group");
请帮助我了解我做错了什么,我想使用laravel集合方法groupby将我的数据库结果数组按"age_gr
我有一个表,我想按用户检索数据组,但是我想要结果中的最新条目,如何使用雄辩的方法做到这一点。
这里是我正在使用的一个雄辩的查询。
Product::whereNotNull('user_id')
->orderBy('id','desc')
->groupBy('user_id')
->get();
这是我的桌子
Id Name user_id
-------------------------
1 A 1
2 b
我想得到谁是发送聊天的联系人id,或者是我用原生查询发送聊天给他,我可以得到结果,但当我在laravel中实现它很困难,这是我的原生查询
select * from `users` where `users`.`id` in (
select `to` from messages where `from` = 2 group by `to`
union
select `from` from messages where `to` = 2 group by `from`
)
我发现困难的是如何在group by或group by之后使用具有相同列号的联合,我使用了
我有一个包含以下列的"messages“表
CREATE TABLE `messages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fromId` int(11) NOT NULL,
`toId` int(11) NOT NULL,
`message` text NOT NULL,
`status` int(11) NOT NULL,
`device` varchar(100) NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,