Laravel 如何将数组转化对应的字符?

例如分类多选IDs[3, 6, 10] 转换对应 name[name3, name6, name10]
寻求简洁的方法可在其它地方调用,例如在App\Http\Resources 设置?
谢谢!

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案
...
protected $appends = [
        'categories_name',
];

public function getCategoriesNameAttribute()
{
    return Category::whereIn('id', $this->categories)->pluck('name')->toArray(); 
}

4年前 评论
讨论数量: 5

可以在resource里面改写$this的值

4年前 评论

@arukas 谢谢! 'category' => $this->category->name //只有一个值name=3可替换,但name[3, 6, 10] 数组无法直接提换?

4年前 评论
...
protected $appends = [
        'categories_name',
];

public function getCategoriesNameAttribute()
{
    return Category::whereIn('id', $this->categories)->pluck('name')->toArray(); 
}

4年前 评论
    $array=[1,2,3];
    $newArr = array_map(function ($value){
        return "name".$value;
    },$array);
    dd($newArr);
4年前 评论
sanders

以上几楼的回答的都不错,不过我常用的方案是用语言包:

__('module.action.'.data_get($this->resource,'id', $defaultId));
4年前 评论
小猪蹄子 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!