stefan7
5 months ago
103
5
Laravel

Remove entry of collection

Posted 5 months ago by stefan7

Hi,

I have a 3-level tree. The function should return json with only 3rd-level catgories for autocomplete field. Autocomplete has no results with $data->forget($key), but why? I tried the code without the return, just printing with dd() and the collection showed 3 items. If I do the same request with the following function i get no results.

I use easyAutocomplete for this.

 public function autocompleteSearch(Request $request)
    {
        $searchquery = $request->searchquery;
        $data = Category::where('parent_id','!=',0)
                          ->where('de_name','like','%'.$searchquery.'%')
                          ->get();
        
        foreach($data as $key => $cat) {
            $category = Category::find($cat->parent_id);
            if($category->parent_id == 0) {
                $data->forget($key);
            }
        }
        return response()->json($data);
    }

Please sign in or create an account to participate in this conversation.