IgorArnaut wrote a reply+100 XP
1d ago
Any easier non-convoluted ways?
IgorArnaut wrote a reply+100 XP
3d ago
IgorArnaut started a new conversation+100 XP
3d ago
Listing is related to Apartment and Apartment is related to Building.
class Listing extends Model
{
// ... fillable, casts
public function apartment(): BelongsTo
{
return $this->belongsTo(Apartment::class);
}
// ... other relations and functions
}
class Apartment extends Model
{
protected $fillable = [
"unit",
"room_count",
"area",
"floor",
"condition",
"heating",
"furnishings",
"ac",
"phone",
"television",
"internet",
"building_id"
];
public $timestamps = false;
// ... casts
public function building(): BelongsTo
{
return $this->belongsTo(Building::class);
}
public function listings(): HasMany
{
return $this->hasMany(Listing::class);
}
// ... other functions
}
class Building extends Model
{
protected $fillable = [
"city",
"street",
"floor_count",
"elevator",
"parking",
"garage",
"cctv",
"intercom"
];
public $timestamps = false;
// ... casts
public function apartments(): HasMany
{
return $this->hasMany(Apartment::class);
}
// ... other functions
}
I access tables apartments and buildings using with method.
class ListingService
{
use ListingOrderBy, ListingWhere;
private const PAGE_SIZE = 2;
function findAll(string $order)
{
$listings = Listing::query()->with(["apartments", "buildings", "users"]);
return $listings->paginate(self::PAGE_SIZE, pageName: "strana");
}
// ...
}
How do I order by and filter listings by its related table columns? For example, I want to filter listings by city or order listings by area.
trait ListingOrderBy
{
public function orderBy(Builder $listings, string $order)
{
return match ($order) {
"najpopularniji" => $listings->withCount("bookmarks")->orderByDesc("bookmarks_count"),
"veći" => $listings->orderByDesc("bookmarks_count"), // order by $listing->apartment->area
"manji" => $listings->orderByDesc("bookmarks_count"), // order by $listing->apartment->area
"skuplji" => $listings->orderByDesc("price"),
"jeftiniji" => $listings->orderBy("price"),
default => $listings->latest()
};
}
}
trait ListingWhere
{
public function whereCityLike(Builder $listings, array $data)
{
return $listings->when(isset($data["grad"]), function () use ($listings, $data) {
return $listings->whereLike([]); // filter by city from $listing->apartment->building
});
}
IgorArnaut wrote a reply+100 XP
3d ago
I want to have the command OPTION not to generate AGENTS.md and CLAUDE.md.
IgorArnaut started a new conversation+100 XP
1w ago
Please, add an option to generate a Laravel project without AI agents and Claude related files. Some people don't want to use AI.
IgorArnaut wrote a reply+100 XP
3mos ago
This does not fix the issue where empty/null query parameters are not appended in pagination query strings.
IgorArnaut started a new conversation+100 XP
3mos ago
I have submitted a form whose fields can be empty and the resulting query string is pretraga?grad=&broj_soba=&m2_od=&m2_do=&cena_od=&cena_do=.
Filter template (realestate\resources\views\components\listings\list\form.blade.php):
<form class="border border-secondary-subtle rounded-3 p-3 mb-3" action="{{ route('listing_search') }}" method="get" novalidate>
<h3 class="fw-semibold mb-3">Pretraga</h3>
<div class="row gy-3 mb-3">
<div class="col-6 col-md-12">
<div class="form-floating">
<input class="form-control" type="text" id="city" name="grad" maxlength="255">
<label for="city">Grad</label>
</div>
</div>
<div class="col-6 col-md-12">
<div class="form-floating">
<input class="form-control" type="number" id="room_count" name="broj_soba" min="0">
<label for="room_count">Broj soba</label>
</div>
</div>
<div class="col-6 col-md-12">
<div class="input-group">
<div class="form-floating">
<input class="form-control" type="number" id="m2_from" name="m2_od" min="0">
<label for="m2_from">Površina od</label>
</div>
<div class="form-floating">
<input class="form-control" type="number" id="m2_to" name="m2_do" min="0">
<label for="m2_to">Površina do</label>
</div>
</div>
</div>
<div class="col-6 col-md-12">
<div class="input-group">
<div class="form-floating">
<input class="form-control" type="number" id="price_from" name="cena_od" min="0">
<label for="price_from">Cena od</label>
</div>
<div class="form-floating">
<input class="form-control" type="number" id="price_to" name="cena_do" min="0">
<label for="price_to">Cena do</label>
</div>
</div>
</div>
</div>
<input class="btn btn-primary fw-semibold" type="submit" value="Pretražite">
</form>
search passes filtered listings to the view (realestate\app\Http\Controllers\ListingController.php):
public function search(Request $request)
{
$order = $request->query("redosled", "");
$listings = $this->listingServ->filter($request->query(), $order);
// $listings = $this->appends($listings, $request->query());
$data = $this->indexData($listings);
return view("listings.listing_list", $data);
}
filter returns filtered listings page (realestate\app\Services\ListingService.php):
public function filter(array $data, string $order)
{
// 1 Parametrized queries
$query = Listing::query();
$query = ListingFilters::filter($query, $data);
return $this->orderPaginate($query, $order);
}
Functions that filters the listings (realestate\app\Services\ListingFilters.php):
private static function whereCity(Builder $query, array $data)
{
$city = $data["grad"];
// 1 Parametrized queries
return !empty($city) ? $query->whereLike("city", "%{$city}%") : $query;
}
private static function whereRoomCount(Builder $query, array $data)
{
$roomCount = $data["broj_soba"];
// 1 Parametrized queries
return !empty($roomCount) ? $query->where("room_count", "=", (int) $roomCount) : $query;
}
private static function whereArea(Builder $query, array $data)
{
$area_from = $data["m2_od"];
$area_to = $data["m2_do"];
// 1 Parametrized queries
if (!empty($area_from)) $query = $query->where("area", ">=", (int) $area_from);
if (!empty($area_to)) $query = $query->where("area", "<=", (int) $area_to);
return $query;
}
private static function wherePrice(Builder $query, array $data)
{
$price_from = $data["cena_od"];
$price_to = $data["cena_do"];
// 1 Parametrized queries
if (!empty($price_from)) $query = $query->where("price", ">=", (int) $price_from);
if (!empty($price_to)) $query = $query->where("price", "<=", (int) $price_to);
return $query;
}
public static function filter(Builder $query, array $data)
{
$query = self::whereCity($query, $data);
$query = self::whereRoomCount($query, $data);
$query = self::whereArea($query, $data);
$query = self::wherePrice($query, $data);
return $query;
}
Pagination template (realestate\resources\views\components\pagination.blade.php):
@if ($listings->hasPages())
<ul class="pagination mb-3">
<li class="page-item {{ $listings->currentPage() === 1 ? 'disabled' : '' }}">
<a class="page-link" href="{{ $listings->withQueryString()->previousPageUrl() ?? '' }}">Prethodna</a>
</li>
@if ($listings->currentPage() > 2)
<li class="page-item">
<a class="page-link" href="{{ $listings->withQueryString()->url(1) }}">1</a>
</li>
@endif
@if ($listings->currentPage() > 1)
<li class="page-item">
<a class="page-link" href="{{ $listings->withQueryString()->previousPageUrl() }}">{{ $listings->currentPage() - 1 }}</a>
</li>
@endif
<li class="page-item active">
<a class="page-link" href="{{ $listings->withQueryString()->url($listings->currentPage()) }}">{{ $listings->currentPage() }}</a>
</li>
@if ($listings->currentPage() < $listings->lastPage())
<li class="page-item">
<a class="page-link" href="{{ $listings->withQueryString()->nextPageUrl() }}">{{ $listings->currentPage() + 1 }}</a>
</li>
@endif
@if ($listings->currentPage() < $listings->lastPage() - 1)
<li class="page-item">
<a class="page-link" href="{{ $listings->withQueryString()->url($listings->lastPage()) }}">{{ $listings->lastPage() }}</a>
</li>
@endif
<li class="page-item {{ $listings->currentPage() === $listings->lastPage() ? 'disabled' : '' }}">
<a class="page-link" href="{{ $listings->withQueryString()->nextPageUrl() ?? '' }}">Sledeća</a>
</li>
</ul>
@endif
When I hover over pagination links after searching/filtering, their URLs do not include other query parameters.
What dd($request->query()) prints:
array:6 [▼ // app\Http\Controllers\ListingController.php:113
"grad" => null
"broj_soba" => null
"m2_od" => null
"m2_do" => null
"cena_od" => null
"cena_do" => null
]
