mgsmus Hocam deniyorum ancak doğru çalışmıyor. Muhtemelen yanlış yerlerde denedim.
Verdiğiniz kodu nasıl kullanabilirim?
Lütfen yol gösterebilirseniz çok sevinirim.
(farklı varsayonlar da denedim)
Model\Yacht sayfamdaki ilgili kod bloğu:
`public function scopeListFrontEnd($query, $options = []){
extract(array_merge([
'page' => 1,
'perPage' => 10,
'sort' => 'created_at desc',
// 'year' => null,
'startYear' => null,
'endYear' => null,
'type' => null,
'des' => null,
'cabin' => null,
], $options));
if(!is_array ($sort)){
$sort = [$sort];
}
foreach ($sort as $_sort){
if(in_array($_sort, array_keys(self::$allowedSortingOptions))){
$parts = explode(' ', $_sort);
if(count($parts) < 2){
array_push($parts, 'desc');
}
list($sortField, $sortDirection) = $parts;
$query->orderBy($sortField, $sortDirection);
}
}
if($type !== null) {
if($type == [0]){
if(!is_array($type)){
$type = [$type];
}
foreach ($type as $typ){
$query->whereHas('type', function($q) use ($typ){
$q->where('id', '>', $typ);
});
}
}else{
if(!is_array($type)){
$type = [$type];
}
foreach ($type as $typ){
$query->whereHas('type', function($q) use ($typ){
$q->where('id', '=', $typ);
});
}
}
}
if($des !== null) {
if(!is_array($des)){
$des = [$des];
}
$query->when($des, function ($whenQuery) use ($des) {
return $whenQuery->whereHas("destinations", function ($relationQuery) use ($des) {
$relationQuery->whereIn("id", $des);
});
});
}
$lastPage = $query->paginate($perPage, $page)->lastPage();
if($lastPage < $page){
$page = 1;
}
if($startYear || $endYear){
$query
->whereBetween('year', [$startYear, $endYear])
->get();
}
if($cabin){
if($cabin == [6]){
$query->where('cabin', '>', $cabin);
}else if($cabin == [0]){
$query->where('cabin', '>', $cabin);
}else{
$query->where('cabin', '=', $cabin);
}
}
return $query->paginate($perPage, $page);
}`
Filtreleme Yaptığım Search Sayfamdaki ilgili kod bloğu:
function onStart() {
$this->prepareVars();
$this->prepareYears();
$this->prepareCabins();
}
function onFilterYachts() { $this->prepareVars(); }
function prepareVars() {
$options = post('Filter', []);
$this['yachts'] = Yacht::listFrontEnd($options);
$this['type'] = Type::all();
$this['des'] = Destinations::all();
$this['sortOptions'] = Yacht::$allowedSortingOptions;
$this['pages'] = $this['yachts']->lastPage();
$this['page'] = $this['yachts']->currentPage();
}
function prepareYears() {
$years = [];
$this['years'] = $years;
}
function prepareCabins() {
$yachts = Yacht::all();
$cabin = [];
$this['cabin'] = $cabin;
}