Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel relationship search

//Search
public function autocomplete(Request $request)
    {
        $name = $request->get('name');

        $filter = DB::table('filters')
            ->join('filter_values', 'filters.id', '=', 'filter_values.filter_id')
            ->whereNull(['filters.deleted_at', 'filter_values.deleted_at'])
            ->select('filters.name as filter_name',
                'filter_values.name as filter_value_name',
                'filter_values.id as id',
                'filter_values.sort_filter as sort_filter')
            ->limit(15)->orderBy('sort_filter', 'asc')
            ->where('filter_values.name', 'like', '%' . $name . '%')
            ->get()->map(function ($item) {
                $item = (array)$item;
                return [
                    'id' => $item['id'],
                    'name' => AdminService::trans($item['filter_name'], null, true) . ' > ' . AdminService::trans($item['filter_value_name'], null, true),
                ];
            });

        return response()->json($filter);

    }

//Trans
public static function trans($array, $locale = null, $json = false)
    {
        if ($json === true) {
            $array = (array)json_decode($array);
        }

        if (!$locale) {
            $locale = app()->getLocale();
        }
        return $array[$locale];
    }
 
PREVIOUS NEXT
Tagged: #laravel #relationship #search
ADD COMMENT
Topic
Name
6+3 =