小编Fri*_*tra的帖子

Laravel Scout toSearchableArray() 没有被调用?

我使用 Laravel Scout 和 TNTSearch 在我的模型之一(食谱)上使用了工作搜索功能:teamtnt/laravel-scout-tntsearch-driver

我想将相同的搜索功能添加到不同的模型(成分)。我试图通过使用将搜索结果作为数组返回toSearchableArray().。为了进行测试,我在模型中执行了以下操作。

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Ingredient extends Model
{
    use Searchable;

    public $asYouType = true;

    public function recipes() 
    {
        return $this->belongsToMany('App\Recipe');
    }    

    public function toSearchableArray()    
    {
        $array = $this->toArray();

        return $array;
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中我正在尝试这样做:

public function search(Request $request)
{
    $results = Ingredient::search($request->q)->get()->toArray();

    return $results;
}
Run Code Online (Sandbox Code Playgroud)

但是,我仍然将我的数据作为集合返回。我正在为我的其他模型(Recipe)使用类似的设置,它确实返回了预期的结果数组。

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Recipe extends Model
{

    use Searchable;

    public $asYouType = true;

    public function ingredients()
    { …
Run Code Online (Sandbox Code Playgroud)

arrays collections toarray laravel laravel-scout

3
推荐指数
1
解决办法
3544
查看次数

Return observable from async function

TLDR; I have a Promise collection().add() which resolves to an object DocumentReference with a listener function onSnapshot() that emits data.

I need to return an Observable which, upon subscription, calls the Promise, subscribes to the listener function on the resolved object, and broadcasts the values from the listener to subscriber.next()

Context: I am trying to declare a function that creates a Firestore document, then returns an Observable with the document data, using the onSnapshot() function on its DocumentReference returned by …

javascript promise observable async-await firebase

3
推荐指数
1
解决办法
2998
查看次数

从 Eloquent 查询更改对象数组中的键

我有一个来自 Eloquent 查询的对象数组:

[
    {
        "id": 1,
        "user_id": 1,
        "name": null,
        "link": "storage/images/foo.png"       
    },
    {
        "id": 2,
        "user_id": 1,
        "name": null,
        "link": "storage/images/foo.png"
    },
    {..}
]
Run Code Online (Sandbox Code Playgroud)

在 PHP 中,将数组中每个对象中的“link”键重命名为“url”的最有效方法是什么?保持元素的相同顺序将是一个奖励。

我希望实现这一目标:

[
    {
        "id": 1,
        "user_id": 1,
        "name": null,
        "url": "storage/images/foo.png"       
    },
    {
        "id": 2,
        "user_id": 1,
        "name": null,
        "url": "storage/images/foo.png"
    },
    {..}
]
Run Code Online (Sandbox Code Playgroud)

php database arrays object laravel

2
推荐指数
1
解决办法
2827
查看次数

按值数组过滤对象数组中的嵌套数组

考虑下面的对象数组:

[
    {
        "guid": "j5Dc9Z",            
        "courses": [
            {
                "id": 1,
                "name": "foo",                    
            }
        ]
    },
    {
        "guid": "a5gdfS",
        "courses": [
            {
                "id": 2,
                "name": "bar",                    
            },
            {
                "id": 3,
                "name": "foo",                    
            },    
        ]
     },
     {
        "guid": "jHab6i",
        "courses": [
            {
                "id": 4,
                "name": "foobar",                    
            }   
        ]
     },  
     {...}    
]
Run Code Online (Sandbox Code Playgroud)

我正在尝试过滤对象数组,将嵌套courses数组中的ID 与下面的数组进行比较:

filter.courses = [1,3]
Run Code Online (Sandbox Code Playgroud)

以下行适用于数组中的第n个值:(通过/sf/answers/2894320901/

let fil = filter(this.results, { courses: [{ id: this.filter.courses[n] }]});
Run Code Online (Sandbox Code Playgroud)

但是,我希望做到这一点(下面的伪代码):

let fil = filter(this.results, { courses: [{ id: this.filter.courses }]}); …
Run Code Online (Sandbox Code Playgroud)

javascript arrays object filter lodash

2
推荐指数
1
解决办法
4559
查看次数

性能:findIndex与Array.prototype.map

在2019年,如果我要处理长度在15000以北的对象数组,并且需要按值查找对象的索引,那么以下哪种方法将是性能上最好的选择?

已有六年历史的“答案”:在对象数组中,这是查找属性与搜索匹配的对象索引的最快方法

findIndex

array.findIndex(object => foo === object.id);
Run Code Online (Sandbox Code Playgroud)

Array.prototype.map

array.map(object => object.id).indexOf(foo);
Run Code Online (Sandbox Code Playgroud)

javascript arrays indexing dictionary object

2
推荐指数
1
解决办法
1039
查看次数

按名称在Map中获取值

这应该是一个简单的,但我没有运气.

我正在尝试从HTTP调用中记录授权标头.

console.log(headers);
Run Code Online (Sandbox Code Playgroud)

给我这张地图:

Map(9) {"date" => Array(1), "server" => Array(1), "authorization" => Array(1), "transfer-encoding" => Array(1), "content-type" => Array(1), …}
Run Code Online (Sandbox Code Playgroud)

我如何进入"授权"标题?我试过了:

console.log(headers.authorization);
console.log(headers[3]);
Run Code Online (Sandbox Code Playgroud)

两者都返回undefined

javascript typescript ecmascript-6

0
推荐指数
1
解决办法
38
查看次数