close
IgorArnaut's avatar

IgorArnaut wrote a reply+100 XP

1d ago

IgorArnaut's avatar

IgorArnaut started a new conversation+100 XP

3d ago

Order by and filter by related table attribute with Eloquent

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 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's avatar

IgorArnaut wrote a reply+100 XP

3d ago

Option to generate project without AI agents and Claude

I want to have the command OPTION not to generate AGENTS.md and CLAUDE.md.

IgorArnaut's avatar

IgorArnaut started a new conversation+100 XP

1w ago

Option to generate project without AI agents and Claude

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's avatar

IgorArnaut wrote a reply+100 XP

3mos ago

Empty query parameters are not appended with withQueryParams

This does not fix the issue where empty/null query parameters are not appended in pagination query strings.

IgorArnaut's avatar

IgorArnaut started a new conversation+100 XP

3mos ago

Empty query parameters are not appended with withQueryParams

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):

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):

Pagination template (realestate\resources\views\components\pagination.blade.php):

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
]