app/maguttiCms/Admin/Controllers/SuggestAjaxController.php
/**
     * This method is used to perform a search for select 2 suggestion fields.
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
This method is used to perform a search for select 2 suggestion fields.
This method is used to perform a search for select 2 suggestion fields .
public function suggest(Request $request)
    {
        $request->validate([
            'q' => 'required',
            'model' => 'required',
            'value' => 'required',
            'caption' => 'required',
        ]);
        $term = $request->q;
        $model = getModelFromString($request->model);
        $value = $request->value;
        $caption = $request->caption;
        $searchField = (isset($request->searchFields) && $request->searchFields != '') ? $request->searchFields : $caption;
        if (!$this->validateRequest($model, $caption)) return response()->json([]);
        if ($this->isTranslatableField($model, $caption)) {
            $records = $model::select('*')->whereTranslationLike($caption, "%{$term}%");
        } else {
            $records = $model::select('*');
            $searchFieldsList = explode(',', $searchField);
            foreach ($searchFieldsList as $_field) {
                $records->orWhere($_field, 'like', "%{$term}%");
            }
        }
        if ($request->has('where') && $request->where != '') {
            $records = $records->whereRaw($request->where);
        }
        $items = $records->get()->map(function ($record) use ($value, $caption) {
            return ['id' => $record->$value, 'value' => $record->$caption];
        });
        return response()->json($items);
    }
public function suggest ( Request $ request ) { $ request -> validate ( [ 'q' => 'required' , 'model' => 'required' , 'value' => 'required' , 'caption' => 'required' , ] ) ; $ term = $ request -> q ; $ model = getModelFromString ( $ request -> model ) ; $ value = $ request -> value ; $ caption = $ request -> caption ; $ searchField = ( isset ( $ request -> searchFields ) && $ request -> searchFields != '' ) ? $ request -> searchFields : $ caption ; if ( ! $ this -> validateRequest ( $ model , $ caption ) ) return response ( ) -> json ( [ ] ) ; if ( $ this -> isTranslatableField ( $ model , $ caption ) ) { $ records = $ model :: select ( '*' ) -> whereTranslationLike ( $ caption , " % { $ term } % " ) ; } else { $ records = $ model :: select ( '*' ) ; $ searchFieldsList = explode ( ',' , $ searchField ) ; foreach ( $ searchFieldsList as $ _field ) { $ records -> orWhere ( $ _field , 'like' , " % { $ term } % " ) ; } } if ( $ request -> has ( 'where' ) && $ request -> where != '' ) { $ records = $ records -> whereRaw ( $ request -> where ) ; } $ items = $ records -> get ( ) -> map ( function ( $ record ) use ( $ value , $ caption ) { return [ 'id' => $ record -> $ value , 'value' => $ record -> $ caption ] ; } ) ; return response ( ) -> json ( $ items ) ; }
This method is used to perform a search for select 2 suggestion fields.
This method is used to perform a search for select 2 suggestion fields .
// Validate the request. // Set convenience variables. // Check if the field is translatable. // This is the simplest case where we know exactly what's the field to search.