/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { // $category = $request->category; $status = $request->status; // datatable parameter $draw = $request->draw; $start = $request->start; $length = $request->length; $search = $request->search['value']; // sorting $column = 'id'; $sort = $request->order[0]['dir'] ? $request->order[0]['dir'] : 'desc'; //asc // new object $properties = new Property(); // with user $properties = $properties->with('user'); // with locale $properties = $properties->with(['propertyLocales' => function ($q) { $q->where('locale', 'en'); }]); // with image $properties = $properties->with(['attachments' => function ($q) { $q->where('type', 'img'); }]); $properties = $properties->with('thumb'); // searching if ($search) { $properties = $properties->select('properties.*')->join('property_locales', 'property_locales.property_id', '=', 'properties.id')->where('property_locales.locale', 'en')->where('property_locales.title', 'like', $search . '%'); } $properties = $properties->orderBy('properties.' . $column, $sort); // villa or land, .. if ($category) { $properties = $properties->select('properties.*')->join('property_terms', 'property_terms.property_id', '=', 'properties.id')->join('terms', 'terms.id', '=', 'property_terms.term_id')->where('terms.slug', $category); } // availbale, un ,... if ($status) { $properties = $properties->where('properties.status', statusToInteger($status)); } // access $properties = $properties->access(); // total records $count = $properties->count(); // pagination $properties = $properties->take($length)->skip($start); // get data $properties = $properties->get(); // datatable response $respose = ["draw" => $draw, "recordsTotal" => $count, "recordsFiltered" => $count, "data" => $properties]; return $respose; }
/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request, $category, $status) { // $category = \App\Category::where('route', $category)->first(); $draw = $request->draw; $start = $request->start; $length = $request->length; $search = $request->search['value']; $column = 'created_at'; $status = statusToInteger($status); $sort = $request->order[0]['dir'] ? $request->order[0]['dir'] : 'desc'; //asc $properties = Property::with(['propertyLanguages' => function ($q) { $q->where('locale', 'en')->orderBy('title', 'asc'); }])->with(['propertyFiles' => function ($q) { $q->where('type', 'image'); }])->with('user')->filterCategory($category); if ($search) { $properties = $properties->select('Properties.*')->join('PropertyLanguages', 'PropertyLanguages.property_id', '=', 'Properties.id')->where('PropertyLanguages.locale', 'en')->where('PropertyLanguages.title', 'like', $search . '%'); } else { $properties = $properties->select('Properties.*')->leftJoin('PropertyLanguages', 'PropertyLanguages.property_id', '=', 'Properties.id')->where('PropertyLanguages.locale', 'en'); } if ($request->order[0]['column']) { $column = $request->columns[$request->order[0]['column']]['data']; //property_languages.0.title if ($column == 'user') { $column = 'user_id'; } if ($column == 'property_languages') { $properties = $properties->orderBy('PropertyLanguages.title', $sort); } else { $properties = $properties->orderBy('Properties.' . $column, $sort); } } else { $properties = $properties->orderBy('Properties.' . $column, $sort); } $properties = $properties->where('status', $status); $count = $properties->count(); $properties = $properties->take($length)->skip($start)->get(); $output = ["draw" => $draw, "recordsTotal" => $count, "recordsFiltered" => $count, "data" => $properties]; return $output; }